<?php namespace App;


/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 6/6/16
 * Time: 1:52 PM
 */
class SlackFire
{

    const API_SEND		= 'https://hooks.slack.com/services/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';
    const API_SEND_INTERACTIVE   = 'https://hooks.slack.com/services/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';
    private $slack;
    private $payload = array();

    /**
     * SlackFire constructor.
     * @param SlackModal $slackModel
     */
    public function __construct(SlackModal $slackModel){
        $this->slack = $slackModel;
        $this->payload();
    }

    /**
     *
     */
    public function payload(){
        //channel name or user
        if($channel = $this->slack->getChannel())
            $this->payload['channel'] = $channel;

        //username or header name of message
        if($username = $this->slack->getUsername())
            $this->payload['username'] = $username;

        //body of message
        if($text = $this->slack->getText())
            $this->payload['text'] = $text;

        //icon_emoji for icon of message
        if($icon_emoji = $this->slack->getIconEmoji())
            $this->payload['icon_emoji'] = $icon_emoji;

        //icon for message
        if($icon_url = $this->slack->getIconUrl())
            $this->payload['icon_url'] = $icon_url;

        //attachment
        if($attachment = $this->slack->getAttachment())
            $this->payload['attachments'] = $attachment;
    }

    /**
     * @return mixed
     */
    public function send(){

        $ch = curl_init();

        $curlConfig = array(
            CURLOPT_URL            => $this->getURL(),
            CURLOPT_POST           => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS     => json_encode($this->payload),
        );

        curl_setopt_array($ch, $curlConfig);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public function getURL(){
        $link = ($url = $this->slack->getResponseUrl()) ? $url :
            ($this->slack->getCallbackId() ? $this->getCurrentURL($this->slack->getChannel()) : self::API_SEND);

        return $link;
    }

    public function getCurrentURL($channel){
        switch ($channel){
            case _Slack::CONCIERGE:
                return self::API_SEND_INTERACTIVE_CONCIERGE;
                break;
            case _Slack::INTERNAL_DEV:
                return self::API_SEND_INTERACTIVE_INTERNAL_DEV;
                break;
            case _Slack::BD:
                return self::API_SEND_INTERACTIVE_BD;
                break;
            case _Slack::ACCOUNT:
                return self::API_SEND_INTERACTIVE_ACCOUNT;
                break;
            case _Slack::TECH_BUG:
                return self::API_SEND_INTERACTIVE;
                break;
            default:
                return self::API_SEND_INTERACTIVE;
                break;
        }
    }

    /**
     * @param $system
     * @return mixed
     */
    public function sendCommand($system){

        $payload = array(
            'text' => $system->sys_service.' '.$system->sys_segment.' '.$system->sys_argument
        );

        $ch = curl_init();

        $curlConfig = array(
            CURLOPT_URL            => $system->response_url,
            CURLOPT_POST           => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
        );

        curl_setopt_array($ch, $curlConfig);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}