<?php namespace App;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Templates;

class CommunicationService
{
    public static $com_category = array(1=>'Consent',2=>'Approval',3=>'Rating',4=>'E-nach',5=>'Welcome Letter',6=>'Overdue');
    public $consent =array();
    public $approval =array();
    public $rating =array();
    public $enach =array();
    public $welcome_letter =array();
    public $overdue =array();

    public function __construct()
    {
        //$this->com_category = CommunicationService::$com_category;
    }

    public function consentArray(){
        $this->consent = array(
            1=>array('id'=>array_keys($this->com_category[0]), 'type'=>'email', 'name'=>$this->com_category[0], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[0]), 'type'=>'sms', 'name'=>$this->com_category[0], 'templateId'=>Templates::$consent_bureau_pull_v1, 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[0]), 'type'=>'whatsapp', 'name'=>$this->com_category[0], 'templateId'=>Templates::$consent_bureau_pull_v1, 'message'=>null)

        );
        return $this->consent;
    }

    public function approvalArray(){
        $this->approval = array(
            1=>array('id'=>array_keys($this->com_category[1]), 'type'=>'email', 'name'=>$this->com_category[1], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[1]), 'type'=>'sms', 'name'=>$this->com_category[1], 'templateId'=>'', 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[1]), 'type'=>'whatsapp', 'name'=>$this->com_category[1], 'templateId'=>'', 'message'=>null)

        );
        return $this->approval;
    }

    public function ratingArray(){
        $this->rating = array(
            1=>array('id'=>array_keys($this->com_category[2]), 'type'=>'email', 'name'=>$this->com_category[2], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[2]), 'type'=>'sms', 'name'=>$this->com_category[2], 'templateId'=>'', 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[2]), 'type'=>'whatsapp', 'name'=>$this->com_category[2], 'templateId'=>'', 'message'=>null)

        );
        return $this->rating;
    }

    public function enachArray(){
        $this->enach = array(
            1=>array('id'=>array_keys($this->com_category[3]), 'type'=>'email', 'name'=>$this->com_category[3], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[3]), 'type'=>'sms', 'name'=>$this->com_category[3], 'templateId'=>'', 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[3]), 'type'=>'whatsapp', 'name'=>$this->com_category[3], 'templateId'=>'', 'message'=>null)

        );
        return $this->enach;
    }

    public function welcomLetterArray(){
        $this->welcome_letter = array(
            1=>array('id'=>array_keys($this->com_category[4]), 'type'=>'email', 'name'=>$this->com_category[4], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[4]), 'type'=>'sms', 'name'=>$this->com_category[4], 'templateId'=>'', 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[4]), 'type'=>'whatsapp', 'name'=>$this->com_category[4], 'templateId'=>'', 'message'=>null)

        );
        return $this->welcome_letter;
    }

    public function overdueArray(){
        $this->overdue = array(
            1=>array('id'=>array_keys($this->com_category[5]), 'type'=>'email', 'name'=>$this->com_category[5], 'templateId'=>null, 'message'=>null),
            2=>array('id'=>array_keys($this->com_category[5]), 'type'=>'sms', 'name'=>$this->com_category[5], 'templateId'=>'', 'message'=>null),
            3=>array('id'=>array_keys($this->com_category[5]), 'type'=>'whatsapp', 'name'=>$this->com_category[5], 'templateId'=>'', 'message'=>null)

        );
        return $this->overdue;
    }

    public function saveCommunicationSetting($id, $communication_setting, $communication_array){
        if(isset($communication_setting) && $communication_setting == 1){
            $communication = CommunicationMapping::where('merchant_id', $id)->get()->toArray();
            //print_r($communication);die;
            $comm_arranged_array = array();
            if(!empty($communication_array)){
                foreach ($communication_array as $value) {
                    $val = explode('-',$value);
                    $type_id = $val[1];
                    $comm_id = $val[0];
                    $comm_arranged_array[$comm_id][] = $type_id;
                }
            }
            if(empty($communication)){
               // print_r($communication);die;
                if(!empty($communication_array)) {
                    foreach($comm_arranged_array as $key => $value){
                        $data = [
                            'merchant_id' =>$id,
                            'type_ids' =>implode(',', $value),
                            'communication_id'=>$key,
                            'created_at' => Carbon::now()->toDateTimeString(),
                            'updated_at' => Carbon::now()->toDateTimeString()
                        ];
                        CommunicationMapping::insert($data);
                    }
                }else{
                    
                }
            }else{
                $comm_array = array(1,2,3);
                $comm_array2 = array();
                if(!empty($communication)){
                    if(!empty($communication_array)) {
                        foreach($comm_arranged_array as $key => $value){
                            if(in_array($key,$comm_array)){
                                $comm_array2[] =$key;
                                $c = CommunicationMapping::where('merchant_id',$id)->where('communication_id',$key)->count();
                                if($c == 0){
                                    $data = [
                                        'merchant_id' =>$id,
                                        'type_ids' =>implode(',', $value),
                                        'communication_id'=>$key,
                                        'created_at' => Carbon::now()->toDateTimeString(),
                                        'updated_at' => Carbon::now()->toDateTimeString()
                                    ];
                                    CommunicationMapping::insert($data);
                                }else{
                                    CommunicationMapping::where('merchant_id',$id)->where('communication_id',$key)->update(['type_ids'=>implode(',',$value)]);
                                }
                            }
                        }
                    }
                    $f_array = array_merge(array_diff($comm_array, $comm_array2), array_diff($comm_array2, $comm_array));
                    foreach($f_array as $val){
                        CommunicationMapping::where('merchant_id',$id)->where('communication_id',$val)->delete();
                    }
                }
            }
        }
    }

    public function getCommunicationSetting($id){
        return CommunicationMapping::where('merchant_id', $id)->get()->toArray();
    }

    public function deleteCommunication($id){
        CommunicationMapping::where('merchant_id', $id)->delete();
    }

}


