<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Schema;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;

class PlotVerificationRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private $field_name;
    private $table_name;
    private $booking;
    private $message;


    public function __construct($table,$field,$booking_id = 0,$parms)
    {
        $this->table_name = $table;
        $this->field_name = $field;
        $this->booking    = $booking_id;
        $this->parms      = $parms;

        $this->message    = 'Required Validation.';

    }

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

            // OLD PLOT ID ITS ONLY WORKS WHEN UPDATE BOOKING AND CHANGE THE PLOT REQUEST
            $old_plot_id = 0;

            // NEW PLOT ID WHEN NEW BOOKING OR UPDATE BOOKING
            $value = decrypt($value);

            // IF BOOKING ID > 0 ITS MEAN UPDATE BOOKING REQUEST
            if($this->getBookingId() != "0")
            {
                $this->booking = decrypt($this->booking);


                $old_plot_id = $this->getBooking()->plot_id;
            }


        
            if($this->verification())
            {
                if($old_plot_id != $value)
                {
                    if($this->getQuery($value) == 1)
                    {
                        return true;
                    }
                    else
                    {
                        $this->setMessage('The '.$attribute.' is invalid');
                        
                    }
                }
                else
                {
                    return true;
                }
                
            }
        } 
        catch (DecryptException $e) 
        {
            //
        }

        return false;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    
    // SET ERROR MESSAGE
    public function setMessage($msg)
    {
        $this->message = $msg;
    }

    // RETURN CUSTOM ERROR MESSAGE
    public function message()
    {
        return $this->message;
    }

    // RETURN FIELD NAME
    public function getFieldName()
    {
        return $this->field_name;
    }

    // RETURN TABLE NAME
    public function getTableName()
    {
        return $this->table_name;
    }

    // RETURN BOOKING ID
    public function getBookingId()
    {
        return $this->booking;
    }


    public function getQuery($value)
    {
        try
        {
             $count = 0;

             $plot_details = DB::table($this->getTableName())->where([$this->getFieldName() => $value, 'status' => 'unsold'])->first();

             if(isset($plot_details->id))
             {
                 // $type      = decrypt($this->parms['type']);
                 // $category  = decrypt($this->parms['category']);
                 $branch_id = decrypt($this->parms['project']);


                 // if($type == $plot_details->type && $category == $plot_details->category && $branch_id == $plot_details->branch_id)
                 // {
                 //      $count = 1;
                 // }

                 if($branch_id == $plot_details->branch_id)
                 {
                      $count = 1;
                 }
             }

             
            
             return $count;

        }
        catch(DecryptException $e)
        {

        }
       

    }

    public function getBooking()
    {
        return DB::table('bookings')->where(['id' => $this->getBookingId()])->first();
    }

    public function verification()
    {
        if(Schema::hasTable($this->getTableName()) && Schema::hasColumn($this->getTableName(),$this->getFieldName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
