<?php

namespace common\modules\rubrication\models;

use common\modules\relation\relationBehavior;
use Yii;
use common\components\artboxtree\ArtboxTreeBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%tax_option}}".
 *
 * @property string $tax_option_id
 * @property integer $tax_group_id
 * @property integer $parent_id
 * @property integer $tree
 * @property string $path_int
 * @property integer $depth
 * @property string $alias
 * @property integer $sort
 * @property integer $default_value
 *
 * @property TaxEntityRelation[] $taxEntityRelations
 * @property TaxGroup $taxGroup
 * @property TaxOption $parent
 * @property TaxOption[] $taxOptions
 * @property TaxOptionToGroup[] $taxOptionToGroups
 * @property TaxOptionToOption[] $taxOptionToOptions
 * @property TaxOptionToOption[] $taxOptionToOptions0
 * @property TaxValueFloat[] $taxValueFloats
 * @property TaxValueInt[] $taxValueInts
 * @property TaxValueString[] $taxValueStrings
 * @property TaxValueText[] $taxValueTexts
 */
class TaxOption extends \yii\db\ActiveRecord
{
    public $_items_count;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'artboxtree' => [
                'class' => ArtboxTreeBehavior::className(),
                'keyNameGroup' => 'tax_group_id',
            ],
            'slug' => [
                'class' => 'common\behaviors\Slug',
                'in_attribute' => 'ValueRenderFlash',
                'out_attribute' => 'alias',
                'translit' => true
            ],
        ];
    }

        /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tax_option}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['tax_group_id'], 'required'],
            [['tax_group_id', 'parent_id', 'sort', 'default_value'], 'integer'],
            [['alias'], 'string', 'max' => 50],
            [['tax_group_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxGroup::className(), 'targetAttribute' => ['tax_group_id' => 'tax_group_id']],
//            [['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['parent_id' => 'tax_option_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'tax_option_id' => Yii::t('app', 'Tax Option ID'),
            'tax_group_id' => Yii::t('app', 'Tax Group ID'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'alias' => Yii::t('app', 'Alias'),
            'sort' => Yii::t('app', 'Sort'),
            'default_value' => Yii::t('app', 'Default Value'),
        ];
    }

    public static function find() {
        return new TaxOptionQuery(get_called_class());
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxEntityRelations()
    {
        return $this->hasMany(TaxEntityRelation::className(), ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGroup()
    {
        return $this->getTaxGroup();
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxGroup()
    {
        return $this->hasOne(TaxGroup::className(), ['tax_group_id' => 'tax_group_id'])->inverseOf('taxOptions');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptions()
    {
        return $this->hasMany(TaxOption::className(), ['parent_id' => 'tax_option_id'])->inverseOf('parent');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToGroups()
    {
        return $this->hasMany(TaxOptionToGroup::className(), ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToOptions()
    {
        return $this->hasMany(TaxOptionToOption::className(), ['tax_option1_id' => 'tax_option_id'])->inverseOf('taxOption1');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTaxOptionToOptions0()
    {
        return $this->hasMany(TaxOptionToOption::className(), ['tax_option2_id' => 'tax_option_id'])->inverseOf('taxOption2');
    }

    /**
     */
    public function getValue()
    {
        $valueClass = $this->getValueModelName();
        if ($valueClass) {
            return $this->hasOne($valueClass, ['tax_value_id' => 'default_value', 'tax_option_id' => 'tax_option_id'])->inverseOf('taxOption');
        }
    }

    /**
     */
    public function getValueRenderFlash()
    {
        $valueClass = $this->getValueModelName();
        $value = $this->value;
        if ($valueClass && method_exists($valueClass, 'getValueRenderFlash')) {
            return $valueClass::getValueRenderFlash($value);
        } elseif(!empty($value)) {
            return $value->value;
        } else {
            return null;
        }
    }

    /**
     */
    public function getValueRenderHTML()
    {
        $valueClass = $this->getValueModelName();
        $value = $this->value;
        if ($valueClass && method_exists($valueClass, 'getValueRenderHTML')) {
            return $valueClass::getValueRenderHTML($value);
        } else {
            return $value->value;
        }
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getValues()
    {
        if ($valueClass = $this->getValueModelName())
            return $this->hasMany($valueClass, ['tax_option_id' => 'tax_option_id'])->inverseOf('taxOption')->cascadeOnDelete();
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            if (empty($this->parent_id))
                $this->parent_id = 0;



            return true;
        }
        return false;
    }

//    public function beforeDelete() {
//        if ( ($model = $this->getValueModelName()) !== FALSE ) {
//
//        }
//    }

    private function getValueModelName() {
        $group = $this->taxGroup;
        $valueClass = '\common\modules\rubrication\models\TaxValue'. ucfirst($group->module);
        return class_exists($valueClass) ? $valueClass : FALSE;
    }
}
