<?php
namespace common\modules\triggers\components\conditions\vendor;

use common\modules\triggers\components\conditions\Conditions;
use yii\helpers\Html;

class ConditionBase {
    protected static $conditionClass;

    public $recall = true;

    public $name;

    public $config = [];

    const MESSAGE_READ = 'ok_read';
    const MESSAGE_LINK_VISITED = 'ok_link_visited';
    const MESSAGE_UNSUBSCRIBED = 'ok_unsubscribed';
    const MESSAGE_SPAM_FOLDER = 'ok_spam_folder';
    const MESSAGE_DELIVERED = 'ok_delivered';

    /**
     * @param string $conditionName
     * @return mixed
     */
    public static function init($conditionName=__CLASS__){
        return self::$conditionClass=new $conditionName(null);
    }

    /**
     * @return mixed
     */
    public function getId(){
        $class=self::$conditionClass;
        return $class::CONDITION_ID;
    }

    /**
     * @param $constant
     * @return bool
     */
    public static function defined($constant){
        return (defined('static::'.$constant)) ? true : false;
    }

    /**
     * @return string
     */
    public function getName(){
        if (property_exists($this, 'name') && $this->name!==null) {
            return $this->name;
        } else {
            return 'Name not found';
        }
    }

    /**
     * @param $group_key
     * @param $condition_key
     * @param array $values
     * @return array
     */
    public function getParams($group_key, $condition_key, $values=array()){
        $result = [];
        if (isset($this->config['params'])) {
            foreach($this->config['params'] as $name=>$param) {
                $type = (empty($param['type'])) ? 'text' : $param['type'];
                switch($type) {
                    case 'text':
                        $result[] = '<input type="text" value="'.((array_key_exists($name, $values)) ? $values[$name] : '').'" name="Conditions['.$group_key.']['.$this->getId().$condition_key.'][params]['.$name.']" class="form-control" style="margin-top: 5px;" placeholder="'.((isset($param['placeholder'])) ? $param['placeholder'] : '').'"/>';
                        break;
                    case 'dropdownlist':
                        $list  = '<select class="form-control" style="margin-top: 5px;" name="Conditions['.$group_key.']['.$this->getId().$condition_key.'][params]['.$name.']">';
                        if (array_key_exists('items',$param)) {
                            foreach($param['items'] as $key=>$value) {
                                $list .= '<option'.(((array_key_exists($name, $values) && $values[$name]==$key) ? ' selected' : '')).' value="'.$key.'">'.$value.'</option>';
                            }
                        }
                        $list .= '</select>';
                        $result[] = $list;
                        break;
                    case 'date':
                        $result[] = '<input data-type="date" type="text" value="'.((array_key_exists($name, $values)) ? $values[$name] : '').'" name="Conditions['.$group_key.']['.$this->getId().$condition_key.'][params]['.$name.']" class="datepicker-autoClose form-control" style="margin-top: 5px;" placeholder="'.((isset($param['placeholder'])) ? $param['placeholder'] : '').'"/>';
                        break;
                    default:
                        $result[] = '<input type="text" value="'.((array_key_exists($name, $values)) ? $values[$name] : '').'" name="Conditions['.$group_key.']['.$this->getId().$condition_key.'][params]['.$name.']" class="form-control" style="margin-top: 5px;" placeholder="'.((isset($param['placeholder'])) ? $param['placeholder'] : '').'"/>';
                        break;
                }
            }
        }
        return $result;
    }
}