<?php

namespace App\Rules;

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

class AgreedAmountRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
  
      private $parms;
      private $sarsai_flag;
      private $size_flag;
      private $feature_flag;
      private $message;



    public function __construct($parms)
    {

            $this->parms = $parms;

            $this->sarsai_flag = false;
            $this->size_flag = false;
            $this->feature_flag = false;

            $this->setMessage("The Agreed Amount is Invalid");

    }

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

        $label_list = EF::getWhiteLabel();
   
        $size = $this->parms[$label_list['size']];
        $plot_details = $this->getPlotById();
        if(isset($plot_details->id))
        {
        //           if(isset($size) && strlen($size) > 0)
        // {
            $total_amount = $plot_details->rate;
            $feature_charges = $plot_details->featurecharges;

            $agreed_amount = (($total_amount + $feature_charges) - (int)$this->parms[$label_list['discount']]);

            if($agreed_amount == $value)
            {
                return true;
            }
            else
            {
                $this->setMessage("The Agreed Amount is Invalid");
            }
        // }
        }
        else 
        {
            return false;
        }

   
        return false;
       
    }

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

   
     //SET SARSAI FLAG 
     public function setSarsai_flag($flag)
    {
        $this->sarsai_flag = $flag;
    }

     //RETURN SARSAI FLAG
    public function getSarsai_flag()
    {
       return $this->sarsai_flag;
    }

     //SET SIZE FLAG
    public function setSize_flag($flag)
    {
        $this->size_flag = $flag;
    }

    //RETURN SIZE FLAG
    public function getSize_flag()
    {
       return $this->size_flag;
    }

     //SET FEATURE FLAG
    public function setfeature_flag($featureflag)
    {
        $this->feature_flag = $featureflag;
    }

     //RETURN FEATURE FLAG 
    public function getfeature_flag()
    {
       return $this->feature_flag;
    }


        // SET ERROR MESSAGE
    public function setMessage($msg)
    {
        $this->message = $msg;
    }

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

        $label_list = EF::getWhiteLabel();
         $plot = new Plot;
         $plot_id = decrypt($this->parms[$label_list['plot']]);
           return   $plot->getPlotById($plot_id);
  
        }
        catch(DecryptException $e)
        {

        }
        

    }

}
