<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 26.01.16
 * Time: 17:03
 */

namespace common\modules\triggers\controllers;

use common\modules\triggers\components\conditions\conditions\UserSuccessfullyCompletedTheTest;
use common\modules\triggers\components\conditions\conditions\UserUnsuccessfullyCompletedTheTest;
use common\modules\triggers\models\TriggerCondition;
use Yii;
use common\modules\triggers\components\conditions\Conditions;
use common\modules\triggers\components\conditions\vendor\ConditionBase;
use common\components\AdminController;
use common\modules\triggers\models\TriggerTrigger;
use yii\data\ActiveDataProvider;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use \common\components\UnisenderAPI;

class TriggerAdminController extends AdminController {
    /**
     * @return array|void
     */
    public static function actionsTitles(){
        return [
            'Manage' => 'Управление триггерами',
            'Create' => 'Добавление триггера',
            'Update' => 'Редактирование триггера',
            'Delete' => 'Удаление триггера',
            'Getconditionhtml' => 'Получаем html блок с условием',
            'Getandconditionhtml' => 'Получаем html новой строки с условиями',

            'Testing' => 'Testing page',
            'Getresult' => 'Testing page',
        ];
    }

    public function actionGetandconditionhtml(){
        $result = Conditions::getControlButtons();
        return (Yii::$app->request->isAjax) ? Json::encode($result) : $result;
    }

    public function actionGetconditionhtml($id, $key){
        $result = Conditions::getConditionHtml($id, $key);
        return Json::encode($result);
    }

    public function actionTesting(){
//        $trigger = \common\modules\triggers\components\conditions\conditions\CheckClickingOnTheLink::init();
//        $trigger->model = TriggerCondition::find()->where(['id' => 11])->one();
//        $model = \common\modules\triggers\models\TriggerSchedule::find()->where(['id'=>24])->one();
//        $trigger->check($model);


//        return $this->render(
//            'testing'
//        );
    }

    public function actionGetresult($id){
        $obj = new UnisenderAPI();
        $result = $obj->getVisitedLinks($id);
        $visited = $obj->linkIsVisited('bystrov@kupitsite.ru', 'http://www.google.com/');
        var_dump($visited);
    }

    /**
     * @return string
     */
    public function actionManage(){
        $dataProvider = new ActiveDataProvider([
            'query' => TriggerTrigger::find()
        ]);

        return $this->render(
            'manage',
            [
                'dataProvider' => $dataProvider
            ]
        );
    }

    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate() {
        $model = new TriggerTrigger();

        if ($model->load(Yii::$app->request->post())) {
            $model->conditions = Yii::$app->request->post('Conditions');
            $datetime=new \DateTime();
            $model->owner_id = Yii::$app->user->getId();
            $model->date_create=$datetime->format('Y-m-d H:i:s');
            if ($model->save())
                return $this->redirect(
                    [
                        'manage'
                    ]
                );
        }
        $form = new \common\components\BaseForm('/common/modules/triggers/forms/TriggerForm', $model);
        return $this->render(
            'create',
            [
                'model' => $model,
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var TriggerTrigger $model */
        $model = TriggerTrigger::findOne($id);
        $model->setScenario('update');

        if ($model->load(Yii::$app->request->post())) {
            $model->conditions = Yii::$app->request->post('Conditions');
            $datetime=new \DateTime();
            $model->owner_id = Yii::$app->user->getId();
            $model->date_create=$datetime->format('Y-m-d H:i:s');
            if ($model->save())
                return $this->redirect(
                    [
                        'manage'
                    ]
                );
        } else {
            $form = new \common\components\BaseForm('/common/modules/triggers/forms/TriggerForm', $model);
            return $this->render(
                'update',
                [
                    'model' => $model,
                    'form' => $form
                ]
            );
        }
    }

    /**
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDelete($id){
        $this->findModel($id)->delete();

        return $this->redirect(['manage']);
    }

    /**
     * @param $id
     * @return null|TriggerTrigger
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = TriggerTrigger::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}