<?php

namespace App\Core\Validator\PropertyChannelMapping;

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;

use App\Core\Validator\BaseValidator;

use Exception;

class PropertyChannelSetupValidator extends BaseValidator
{



    public function __construct
    (

        Translator $translator,
        Container $container
    )
    {
        parent::__construct($translator, $container);

    }

    public function rules($params = null)
    {
        $rules =
            [
                'property_id' => 'required|numeric',
                'channel_id' => 'required|numeric',
                'checkPayment' => 'accepted',
                'booking' => 'array',
                'payment' => 'array',
                'availability' => 'array',
                'availability.availability_type_id' => 'numeric',

            ] ;


        if(isset($params['booking']['value'])){
            $optionalRules = [
                'booking.booking_type_id' => 'required|numeric',
                'booking.action_type' => 'required|in:INC,DEC',
                'booking.value_type' => 'required|in:FIX,PER',
                'booking.value' => 'numeric',
            ] ;
            $rules =  array_merge($rules, $optionalRules) ;
        }

        return $rules ;


    }

    public function messages()
    {
        $thisMessages = [];
        return array_merge(parent::messages() , $thisMessages);
    }


    public function paymentValueControl($params){
        try {
            $errorStatus = false ;

            foreach ($params['payment'] as $payment) {
                if(
                    ($payment['value']) &&
                    (
                         $payment['action_type'] == null || $payment['value_type'] == null ||
                         !in_array($payment['action_type'], ['INC', 'DEC']) || !in_array($payment['value_type'], ['FIX', 'PER'])

                    )
                ){
                    $errorStatus = true ;
                    break;
                }
            }
            return $errorStatus ? false : true;
        } catch (Exception $e) {
            return false;
        }


    }

    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["checkPayment"] = $this->paymentValueControl($params);
        return $this->make($params, $this->rules($params), $this->messages());
    }

}
