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

/**
 * Created by PhpStorm.
 * @author: Sunil Khorwal
 * Date: 6/12/16
 * Time: 6:01 PM
 */
class SlackListener{

    public $attachment_title;
    public $channel;
    public $attachment_text;
    public $socket_body = '';
    public $method;
    public $pure_fields;
    public $pure_fields_values;
    public $callback_id;
    // This feature is coded to prevent unwanted request from user space
    private $supported_methods = array(
        'newReservation','cancelReservation','editReservation',
        'giftCardPurchase','optionalPaymentMade','partyRequest',
        'experienceDiscontinued','paymentReceive','paymentTransfer',
        'sendMerchantLoginActivity','listingIssue','reservationRejected','reservationContext',
        'loginFailed','noShowDispute','unSuccessfulTransaction','prePaymentMade','postReviews','netPromoter', 'requestCallback','managerAppUninstall',
        'sendCampaignDetails'
    );
    /**
     * @var Request
     */
    private $request;
    private $socket;

    /**
     * Reservation constructor.
     * @param Request $request
     */
    public function __construct(Request $request){

        $this->request = $request;
    }

    /**
     * @return Request
     */
    public function getReservationSegments(){
        $messages = array(
            'channel' => $this->channel,
            'attachment_title' => $this->attachment_title,
            'attachment_text' => $this->attachment_text,
            'pure_fields' => $this->pure_fields,
            'pure_fields_values' => $this->pure_fields_values,
            'instance_type' => $this->request->get('instance_type'),
            'callback_id' => $this->callback_id
        );

        if($this->callback_id)
            $messages = _Slack::InteractiveMessage($messages,$this->callback_id);

        return new Request($messages);
    }

    /**
     * @param $channel
     * @param $method
     * @param $title
     */
    public function setSlackSegments($channel, $method){
        $this->channel = $this->request->get('channel');
        $this->attachment_title = $this->request->get('attachment_title');
        $actionFields =  $this->actionFields($method);
        if(!empty($actionFields)){
            $this->attachment_text = $this->request->has('attachment_text') ? $this->request->get('attachment_text') : '';
            $this->pure_fields = true ;
            $this->pure_fields_values = $actionFields;
        }else{
            $this->attachment_text =  'Please check bookings for details';
        }
    }

    /**
     * @param $method
     */
    public function setReservationSegments($method){
        $this->method = $method;

        $this->callback_id = $this->request->has('callback_id') ? $this->request->get('callback_id') : '';
        $this->channel = 'https://hooks.slack.com/services/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';
        //$title = 'New app registration in '.$this->request->get('city');
        $this->setSlackSegments($this->channel,$this->method);

        //socket message
        $this->socket();
        //end socket message
    }

    /**
     * @return array
     */
    public function socket(){
        return [
            'socket_title'=>$this->attachment_title,
            'socket_body'=>($this->socket_body) ? $this->socket_body : (isset($this->socket['Date']) ? $this->socket['Date'] : '')." ".(isset($this->socket['Time']) ? $this->socket['Time'] : '')
        ];
    }

    /**
     * @param $method
     * @return array
     */
    public function actionFields($method){
        $response =  array();
        if($method) {
            if ($this->request->has('attributes') && !empty($this->request->get('attributes'))){
                foreach ($this->request->get('attributes') as $attribute=>$attribute_value){
                    $response[] = $this->getFieldsValue($attribute_value, $attribute);
                }
            }

        }
        return $response;
    }

    /**
     * @param $name
     * @param $title
     * @return array
     */
    public function getFieldsValue($name, $title){
        $this->initSocket($title,$name);
        return array('fields_title' => $title, 'fields_value' => $name, 'fields_short' => false);
    }

    /**
     * @param $name
     * @param $value
     */
    public function initSocket($name, $value){
        $this->socket[$name] = $name.': '.$value;
    }
}