<?php
namespace frontend\widgets;
use common\models\SeoDynamic;
use yii\base\Widget;
use yii\helpers\Html;

class Seo extends Widget
{
    private $url;
    public  $row;
    public  $own_attr;
    public  $fields;
    public $description;
    public $title;
    public $seo_text;
    public $h1;
    public $key;
    public $project_name;
    const SEO_TEXT = 'seo_text';
    const DESCRIPTION = 'description';
    const H1 = 'h1';
    const TITLE = 'title';

    public function init(){
        $this->url   = \Yii::$app->request->url;
        $this->project_name   = \Yii::$app->name;
        parent::init();

    }


    public function run()
    {

        $seoData = $this->getViewData();
        foreach($seoData as $key=>$value){
            $this->$key = $value;
        }


        switch ($this->row) {
            case self::SEO_TEXT:
                return $this->selectSeoData(self::SEO_TEXT);

                break;
            case self::H1:
                return $this->selectSeoData(self::H1);
                break;
            case self::TITLE:

                $filter =  \Yii::$app->request->get('filter', []);
                if(!empty($filter)){
                    $filter_row = '';
                    foreach($filter as $sub_filter_name => $sub_filter_array){
                        if($sub_filter_name=='options'){
                            foreach($sub_filter_array as $f_name=>$f_values){
                                $filter_row .= $f_name.':'.implode(',',$f_values).'|';
                            }
                        }

                    }
                    $this->fields['name'] = $filter_row;
                }

                $title = $this->selectSeoData(self::TITLE);



                if(!empty($title)){
                    return $title;
                } else {
                    return $this->project_name;
                }

                break;
            case self::DESCRIPTION:
                $description = $this->selectSeoData(self::DESCRIPTION);

                if(!empty($description)){

                    $this->getView()->registerMetaTag([
                        'name' => 'description',
                        'content' =>  $description
                    ]);

                }

                break;
        }


    }

    protected function replaceData($str)
    {

        if(!empty($this->fields)){
            foreach($this->fields as $field_name => $field_value){
                $str = str_replace('{'.$field_name.'}', $field_value, $str);
            }
        }
        $str = str_replace('{project_name}', $this->project_name, $str);
        return Html::encode($str);
    }

    protected function findSeoByUrl()
    {
        return  \common\models\Seo::findOne(['url'=>$this->url]);
    }

    protected function findSeoByDynamic()
    {
        $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id]);
        if(!empty($this->key)){
            $query->andWhere(['key'=>$this->key]);
        }
        return $query->one();
    }


    protected function getViewData(){
        $params = $this->getView()->params;
        if(isset($params['seo'])){
            return $params['seo'];
        } else {
            return [];
        }
    }

    protected function selectSeoData($param){
        $result = '';

        $widgetData = $this->findSeoByUrl();

        if($widgetData instanceof \common\models\Seo){

            $result =  $widgetData->$param;

        }else if(!empty($this->$param)){

            $result = $this->$param;

        } else {

            $widgetData = $this->findSeoByDynamic();

            if($widgetData instanceof SeoDynamic){

                $result =  $widgetData->$param;

            }

        }

        return $this->replaceData($result);

    }


}