<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Schema;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;
use EF;
use Crypt;
use App\Leave;

class LeaveVerificationRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private $parms;
    private $leave_id;

    private $message;

    public function __construct($parms,$leave_id)
    {
        $this->parms    = $parms;
        $this->leave_id = $leave_id;
        $this->message  = 'The validation error message.';
        
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

         $sdate         = $this->parms['start_date'];
         $edate         = $this->parms['end_date'];
         $no_of_hours   = $this->parms['no_of_hours'];
         $type          = $this->parms['type'];


         // THIS PART WORK ONLY FOR THE LEAVE UPDATION
        try 
        {
            $leave_id = decrypt($this->leave_id);
            
            $leave = $this->getLeaveById($leave_id);

            if(isset($leave->id))
            {
                $this->parms['employee'] = Crypt::encrypt($leave->user_id);
                
                //CHECK NUMBER OF HOURS
                // if(isset($leave->id) && $this->parms['type'] == "Short Leave")
                // {
                //     if($leave->end_date > date('Y-m-d'))
                //     {

                //         $this->setMessage('Previous Leave is not yet expired.');
                //         return false;
                //     }

                // } 

            }
        }
        catch (DecryptException $e) 
        {
            $this->setMessage('Leave Invalid');

            return false;
        }
        // END THIS PART WORK ONLY FOR THE LEAVE UPDATION



         if((strlen(trim($sdate)) > 0 && strlen(trim($edate)) > 0) && (strlen(trim($no_of_hours)) == 0 && $type != "Short Leave"))
         {
            
            if($this->verification($sdate,$edate))
            {
                $this->setMessage('Leave already found');

                return false;
            }
         }
         elseif((strlen(trim($no_of_hours)) > 0 && $type == "Short Leave") && (strlen(trim($sdate)) == 0 && strlen(trim($edate)) == 0))
         {
            if($this->verification(date('Y-m-d'),date('Y-m-d')))
            {
                $this->setMessage('Leave already found');

                return false;
            }
         }
         else
         {
            $this->setMessage('Date and no of hours invalid');

            return false;
         }

         return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */

    public function setMessage($msg)
    {
        $this->message = $msg;
    }

    public function message()
    {
        return $this->message;
    }

    public function getParmsList()
    {
        return $this->parms;
    }


    public function verification($sdate,$edate)
    {
        // THIS QUERY SATISFIED CONDITION 1 RELATED TO DURATION
        
        try 
        {
            
            $leave_id = decrypt($this->leave_id);

            $user_id = decrypt($this->parms['employee']);
        
            $condition = ['user_id' => $user_id, 'is_recycle' => '0' ];

            $flag1 =    DB::table('leaves')
                        ->where($condition)
                        ->where('id','!=',$leave_id)
                        ->whereDate('start_date','>=',$sdate)
                        ->whereDate('end_date','<=',$edate)
                        ->count();

            $condition = ['user_id' => $user_id, 'is_recycle' => '0','status' => 'Short Leave'];

            $flag2 = DB::table('leaves')
                    ->where($condition)
                    ->where('id','!=',$leave_id)

                    ->count();

            if(!$flag1 && !$flag2)
            {
                return 0;
            }

            return 1;

        }
        catch (DecryptException $e) 
        {
            //
        }

    }

    public function getLeaveById($id)
    {
        $leave =  new Leave;

        return $leave->getLeaveById($id);
    }

}
