<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%fields}}".
 *
 * @property integer $id
 * @property string $table_name
 * @property integer $table_id
 * @property string $value
 * @property string $field_name
 * @property string $field_type
 * @property string $language
 * @property string $parent_key
 * @property string $key
 */
class Fields extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%fields}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['table_name', 'table_id',], 'required'],
            [['table_id','parent_key','key'], 'integer'],
            [['table_name', 'value', 'field_name','field_type','language'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'table_name' => 'Model Name',
            'table_id' => 'Model ID',
            'value' => 'Value',
            'field_name' => 'Field Name',
            'language' => 'Language',
        ];
    }

    public static function getData($id, $model, $type){
        $data = ArrayHelper::toArray(self::find()->where(['table_id'=>$id, 'table_name'=>$model, 'field_type'=>$type])->all());
        $result = [];
        for($i=0; $i < count($data); $i ++){
            $result[$data[$i]['parent_key']][$data[$i]['field_name']] = $data[$i]['value'];
        }

       return $result;
    }


    /**
     * @param $post - array with field data
     * @param $table_id - row id in model table
     * @param $table_name - madel table name
     * @param $language - language id
     */

    public static function saveFieldData($post,$table_id,$table_name, $language){

        self::deleteAll(['table_id'=>$table_id, 'table_name'=>$table_name, 'language' => $language, 'field_type' => array_keys($post)]);

        if($post){


            foreach($post as $k => $field){



                foreach($field as $parent_key => $row){

                    foreach($row as $key => $value){

                        $field_model = new Fields();
                        $field_model->field_name =  array_keys($value)[0];
                        $field_model->value = $value[array_keys($value)[0]];
                        $field_model->table_name =  $table_name;
                        $field_model->table_id   = $table_id;
                        $field_model->field_type =  $k;
                        $field_model->language =  'ru';
                        $field_model->parent_key =  $parent_key;
                        $field_model->key =  $key;
                        $field_model->save();
                    }

                }
            }
        }
    }


    /**
     * @param $post - array with field data
     * @param $table_id - row id in model table
     * @param $table_name - madel table name
     * @param $language - language id
     */

    public static function saveFieldVideoData($post,$table_id,$table_name, $language){

        self::deleteAll(['table_id'=>$table_id, 'table_name'=>$table_name, 'language' => $language, 'field_type' => array_keys($post)]);

        if($post){


            foreach($post as $k => $field){



                foreach($field as $parent_key => $row){

                    foreach($row as $key => $value){

                        preg_match('/src=\"(.[^"]*)\"/', $value[array_keys($value)[0]], $video_url);

                        if(isset($video_url[1]) && !empty($video_url[1])){

                            $field_model = new Fields();
                            $field_model->field_name =  array_keys($value)[0];
                            $field_model->value = $video_url[1].'?showinfo=0&autoplay=0';
                            $field_model->table_name =  $table_name;
                            $field_model->table_id   = $table_id;
                            $field_model->field_type =  $k;
                            $field_model->language =  'ru';
                            $field_model->parent_key =  $parent_key;
                            $field_model->key =  $key;
                            $field_model->save();

                        }


                    }

                }
            }
        }
    }
}
