<?php namespace App;

/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 6/6/16
 * Time: 1:51 PM
 */
class SlackModal
{
    protected $channel;
    protected $username;
    protected $text;
    protected $icon_emoji;
    protected $icon_url;
    protected $fallback;
    protected $color;
    protected $pretext;
    protected $author_name;
    protected $author_link;
    protected $author_icon;
    protected $title;
    protected $title_link;
    protected $attachment_text;
    protected $attachment_title;
    protected $fields = array();
    protected $fields_title;
    protected $fields_value;
    protected $fields_short = true;
    protected $image_url;
    protected $thumb_url;
    protected $footer;
    protected $footer_icon;
    protected $ts;
    protected $callback_id;
    protected $attachment_type;
    protected $actions = array();
    protected $actions_name;
    protected $actions_text;
    protected $actions_type;
    protected $actions_value;
    protected $action_confirm = array();
    protected $action_confirm_title;
    protected $action_confirm_text;
    protected $action_confirm_ok_text;
    protected $action_confirm_dismiss_text;
    protected $isPureFields = false;
    protected $isPureFieldsArray = array();
    protected $response_type;
    protected $replace_original;
    protected $bot;
    protected $response_url;

    /**
     * SlackModal constructor.
     */
    public function __construct(){

    }

    /**
     * @param null $tz
     * @return static
     */
    public static function setup($tz = null){
        return new static();
    }

    /**
     * @param $channel
     * @return $this
     */
    public function setChannel($channel){
        $this->channel = $channel;
        return $this;
    }

    /**
     * @param $username
     * @return $this
     */
    public function setUsername($username){
        $this->username = $username;
        return $this;
    }

    /**
     * @param $text
     * @return $this
     */
    public function setText($text){
        $this->text = $text;
        return $this;
    }

    /**
     * @param $icon_emoji
     * @return $this
     */
    public function setIconEmoji($icon_emoji){
        $this->icon_emoji = $icon_emoji;
        return $this;
    }

    /**
     * @param $icon_url
     * @return $this
     */
    public function setIconUrl($icon_url){
        $this->icon_url = $icon_url;
        return $this;
    }

    /**
     * @param $fallback
     * @return $this
     */
    public function setFallBack($fallback){
        $this->fallback = $fallback;
        return $this;
    }

    /**
     * @param $color
     * @return $this
     */
    public function setColor($color){
        $this->color = $color;
        return $this;
    }

    /**
     * @param $pretext
     * @return $this
     */
    public function setPreText($pretext){
        $this->pretext = $pretext;
        return $this;
    }

    /**
     * @param $author_name
     * @return $this
     */
    public function setAuthorName($author_name){
        $this->author_name = $author_name;
        return $this;
    }

    /**
     * @param $author_link
     * @return $this
     */
    public function setAuthorLink($author_link){
        $this->author_link = $author_link;
        return $this;
    }

    /**
     * @param $author_icon
     * @return $this
     */
    public function setAuthorIcon($author_icon){
        $this->author_icon = $author_icon;
        return $this;
    }

    /**
     * @param $replace_original
     * @return $this
     */
    public function setReplaceOriginal($replace_original){
        $this->replace_original = $replace_original;
        return $this;
    }

    /**
     * @param $response_type
     * @return $this
     */
    public function setResponseType($response_type){
        $this->response_type = $response_type;
        return $this;
    }

    /**
     * @param $response_url
     * @return $this
     */
    public function setResponseUrl($response_url){
        $this->response_url = $response_url;
        return $this;
    }

    /**
     * @param $title
     * @return $this
     */
    public function setTitle($title){
        $this->title = $title;
        return $this;
    }

    /**
     * @param $title_link
     * @return $this
     */
    public function setTitleLink($title_link){
        $this->title_link = $title_link;
        return $this;
    }

    /**
     * @param $attachment_text
     * @return $this
     */
    public function setAttachmentText($attachment_text){
        $this->attachment_text = $attachment_text;
        return $this;
    }

    /**
     * @param $attachment_title
     * @return $this
     */
    public function setAttachmentTitle($attachment_title){
        $this->attachment_title = $attachment_title;
        return $this;
    }

    /**
     * @param $fields_title
     * @return $this
     */
    public function setFieldsTitle($fields_title){
        $this->fields_title = $fields_title;
        return $this;
    }

    /**
     * @param $fields_value
     * @return $this
     */
    public function setFieldsValue($fields_value){
        $this->fields_value = $fields_value;
        return $this;
    }

    /**
     * @param $fields_short
     * @return $this
     */
    public function setFieldsShort($fields_short){
        $this->fields_short = $fields_short;
        return $this;
    }

    /**
     * @param $image_url
     * @return $this
     */
    public function setImageUrl($image_url){
        $this->image_url = $image_url;
        return $this;
    }

    /**
     * @param $thumb_url
     * @return $this
     */
    public function setThumbUrl($thumb_url){
        $this->thumb_url = $thumb_url;
        return $this;
    }

    /**
     * @param $footer
     * @return $this
     */
    public function setFooter($footer){
        $this->footer = $footer;
        return $this;
    }

    /**
     * @param $footer_icon
     * @return $this
     */
    public function setFooterIcon($footer_icon){
        $this->footer_icon = $footer_icon;
        return $this;
    }

    /**
     * @param $bot
     * @return $this
     */
    public function setBot($bot){
        $this->bot = $bot;
        return $this;
    }

    /**
     * @return $this
     */
    public function setTs(){
        $this->ts = time();
        return $this;
    }

    /**
     * @param $callback_id
     * @return $this
     */
    public function setCallbackId($callback_id){
        $this->callback_id = $callback_id;
        return $this;
    }

    /**
     * @param $attachment_type
     * @return $this
     */
    public function setAttachmentType($attachment_type){
        $this->attachment_type = $attachment_type;
        return $this;
    }

    /**
     * @param $action_name
     * @return $this
     */
    public function setActionName($action_name){
        $this->actions_name = $action_name;
        return $this;
    }

    /**
     * @param $action_text
     * @return $this
     */
    public function setActionText($action_text){
        $this->actions_text = $action_text;
        return $this;
    }

    /**
     * @param $action_type
     * @return $this
     */
    public function setActionType($action_type){
        $this->actions_type = $action_type;
        return $this;
    }

    /**
     * @param $action_value
     * @return $this
     */
    public function setActionValue($action_value){
        $this->actions_value = $action_value;
        return $this;
    }

    /**
     * @param $action_confirm_title
     * @return $this
     */
    public function setActionConfirmTitle($action_confirm_title){
        $this->action_confirm_title = $action_confirm_title;
        return $this;
    }

    /**
     * @param $action_confirm_text
     * @return $this
     */
    public function setActionConfirmText($action_confirm_text){
        $this->action_confirm_text = $action_confirm_text;
        return $this;
    }

    /**
     * @param $action_confirm_ok_text
     * @return $this
     */
    public function setActionConfirmOkText($action_confirm_ok_text){
        $this->action_confirm_ok_text = $action_confirm_ok_text;
        return $this;
    }

    /**
     * @param $action_confirm_dismiss_text
     * @return $this
     */
    public function setActionConfirmDismissText($action_confirm_dismiss_text){
        $this->action_confirm_dismiss_text = $action_confirm_dismiss_text;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getChannel(){
        return $this->channel;
    }

    /**
     * @return mixed
     */
    public function getUsername(){
        return $this->username;
    }

    /**
     * @return mixed
     */
    public function getText(){
        return $this->text;
    }

    /**
     * @return mixed
     */
    public function getIconEmoji(){
        return $this->icon_emoji;
    }

    /**
     * @return mixed
     */
    public function getIconUrl(){
        return $this->icon_url;
    }

    /**
     * @return mixed
     */
    public function getReplaceOriginal(){
        return $this->replace_original;
    }

    /**
     * @return mixed
     */
    public function getResponseType(){
        return $this->response_type;
    }

    /**
     * @return mixed
     */
    public function getBot(){
        return $this->bot;
    }

    /**
     * @param $fall_back
     * @return mixed
     */
    public function getFallBack($fall_back){
        return $fall_back;
    }

    /**
     * @return mixed
     */
    public function getResponseUrl(){
        return $this->response_url;
    }

    /**
     * @return mixed
     */
    public function getColor(){
        return $this->color;
    }

    /**
     * @return mixed
     */
    public function getPreText(){
        return $this->pretext;
    }

    /**
     * @return mixed
     */
    public function getAuthorName(){
        return $this->author_name;
    }

    /**
     * @return mixed
     */
    public function getAuthorLink(){
        return $this->author_link;
    }

    /**
     * @return mixed
     */
    public function getAuthorIcon(){
        return $this->author_icon;
    }

    /**
     * @return mixed
     */
    public function getTitle(){
        return $this->title;
    }

    /**
     * @return mixed
     */
    public function getTitleLink(){
        return $this->title_link;
    }

    /**
     * @return mixed
     */
    public function getAttachmentText(){
        return $this->attachment_text;
    }

    /**
     * @return mixed
     */
    public function getFieldsTitle(){
        return $this->fields_title;
    }

    /**
     * @return mixed
     */
    public function getFieldsValue(){
        return $this->fields_value;
    }

    /**
     * @return mixed
     */
    public function getFieldsShort(){
        return $this->fields_short;
    }

    /**
     * @return mixed
     */
    public function getImageUrl(){
        return $this->image_url;
    }

    /**
     * @return mixed
     */
    public function getThumbUrl(){
        return $this->thumb_url;
    }

    /**
     * @return mixed
     */
    public function getFooter(){
        return $this->footer;
    }

    /**
     * @return mixed
     */
    public function getFooterIcon(){
        return $this->footer_icon;
    }

    /**
     * @return mixed
     */
    public function getTs(){
        return $this->ts;
    }

    /**
     * @return mixed
     */
    public function getCallbackId(){
        return $this->callback_id;
    }

    /**
     * @return mixed
     */
    public function getAttachmentType(){
        return $this->attachment_type;
    }

    /**
     * @return mixed
     */
    public function getActionName(){
        return $this->actions_name;
    }

    /**
     * @return mixed
     */
    public function getActionText(){
        return $this->actions_text;
    }

    /**
     * @return mixed
     */
    public function getActionType(){
        return $this->actions_type;
    }

    /**
     * @return mixed
     */
    public function getActionValue(){
        return $this->actions_value;
    }

    /**
     * @return mixed
     */
    public function getActionConfirmTitle(){
        return $this->action_confirm_title;
    }

    /**
     * @return mixed
     */
    public function getActionConfirmText(){
        return $this->action_confirm_text;
    }

    /**
     * @return mixed
     */
    public function getActionConfirmOkText(){
        return $this->action_confirm_ok_text;
    }

    /**
     * @return mixed
     */
    public function getActionConfirmDismissText(){
        return $this->action_confirm_dismiss_text;
    }

    //custom fields
    /**
     * @param $fields
     * @return $this
     */
    public function setPureFields($fields){
        $this->isPureFields = $fields;
        return $this;
    }

    /**
     * @return bool
     */
    public function getPureFields(){
        return $this->isPureFields;
    }

    /**
     * @param $fields
     * @return $this
     */
    public function setPureFieldsArray($fields){
        $this->isPureFieldsArray = $fields;
        return $this;
    }

    /**
     * @return array
     */
    public function getPureFieldsArray(){
        return $this->isPureFieldsArray;
    }

    //custom ends

    /**
     * @return array
     */
    public function getFields(){
        $fields = array();
        if($this->isPureFields) {
            foreach ($this->isPureFieldsArray as $item) {
                $fields[] = array(
                    'title' => isset($item['fields_title']) ? $item['fields_title'] : $item['title'],
                    'value' => isset($item['fields_value']) ? $item['fields_value'] : $item['value'],
                    'short' => true
                );
            }
            return $fields;
        }
        return array(
            'title' => $this->fields_title,
            'value' => $this->fields_value,
            'short' => $this->fields_short
        );
    }

    /**
     * @return array
     */
    public function getActions(){
        if(is_array($this->actions_name)){
            $messages = array();
            foreach ($this->actions_name as $key=>$action){
                $messages[] = array(
                    'name' => $this->actions_name[$key],
                    'text' => $this->actions_text[$key],
                    'type' => $this->actions_type,
                    'value' => $this->actions_value[$key],
                );
            }

            $actions = $messages;

        }else {
            $actions = array(
                array(
                    'name' => $this->actions_name,
                    'text' => $this->actions_text,
                    'type' => $this->actions_type,
                    'value' => $this->actions_value,
                )
            );
        }

        if(!empty($confirm = $this->getActionConfirm()))
            $actions[0]['confirm'] = $confirm;

        return $actions;

    }

    /**
     * @return array
     */
    public function getActionConfirm(){
        if(!empty($this->action_confirm_title)) {
            return array(
                'title' => $this->action_confirm_title,
                'text' => $this->action_confirm_text,
                'ok_text' => $this->action_confirm_ok_text,
                'dismiss_text' => $this->action_confirm_dismiss_text,
            );
        }
        return array();
    }

    /**
     * @return array
     */
    public function getAttachment(){

        $message = array(
            'fallback' => $this->fallback,
            'color' => $this->color,
            'callback_id' => $this->callback_id,
            'pretext' => $this->pretext,
            'author_name' => $this->author_name,
            'author_link' => $this->author_link,
            'author_icon' => $this->author_icon,
            'title' => $this->attachment_title,
            'title_link' => $this->title_link,
            'text' => $this->attachment_text,
            'image_url' => $this->image_url,
            'thumb_url' => $this->thumb_url,
            'footer' => $this->footer,
            'footer_icon' => $this->footer_icon,
            'ts'=> $this->ts
        );

        $attachment =   array(
            $message
        );


        if($this->bot) {
            $attachment[0]['replace_original'] = $this->replace_original;
            $attachment[0]['response_type'] = $this->response_type;
            //$attachment[0]['text'] = $this->text;
        }

        if(!empty($fields = $this->getFields()))
            $attachment[0]['fields'] = $fields;

        if(!empty($actions = $this->getActions()))
            $attachment[0]['actions'] = $actions;

        return $attachment;
    }
}