<?php

namespace Hura8\Components\Product\AdminController;

use Hura8\System\Controller\aAdminEntityBaseController;
use Hura8\Components\Product\Model\ProductAttributeLanguageModel;
use Hura8\Components\Product\Model\ProductAttributeModel;
use Hura8\Components\Product\Model\ProductAttributeValueModel;
use Hura8\System\Security\DataClean;
use Hura8\System\Security\DataType;


class AProductAttributeController extends aAdminEntityBaseController
{
    /* @var ProductAttributeModel $objProductAttributeModel */
    protected $objProductAttributeModel;

    /* @var ProductAttributeLanguageModel $objProductAttributeLanguageModel */
    protected $objProductAttributeLanguageModel;

    protected $view_language = LANGUAGE;

    public function __construct()
    {
        $this->objProductAttributeModel = new ProductAttributeModel();

        if(!$this->isDefaultLanguage()) {
            $this->objProductAttributeLanguageModel = new ProductAttributeLanguageModel($this->view_language);
            //$this->objProductAttributeLanguageModel->createTableLang();

            parent::__construct($this->objProductAttributeModel, $this->objProductAttributeLanguageModel);

        }else{
            parent::__construct($this->objProductAttributeModel);
        }
    }


    public function getProductAttributes($product_id) {
        $objProductAttributeValueModel = new ProductAttributeValueModel(0);

        $result = [];
        foreach ($objProductAttributeValueModel->getProductAttributes($product_id) as $info) {
            $result[$info['attr_id']][] = $info['attr_value_id'];
        }

        return $result;
    }

    public function updateProductAttributes($product_id, array $current_value, array $new_value) {
        /*
         $current_value => Array
                (
                    // attribute_id => [value1, value2]
                    [2] => Array
                        (
                            [0] => 2
                            [1] => 3
                        )

                    [1] => Array
                        (
                            [0] => 1
                        )

                )

        $new_value => Array
            (
                // attribute_id => new_str_values by lines
                [2] => asdas
                [1] => asda
            )
         * */

        // list of values for product
        $product_value_ids = [];

        // use current values
        foreach ($current_value as $_attr_id => $_value_ids) {
            $product_value_ids = array_merge($product_value_ids, $_value_ids);
        }

        // create new values for attributes
        foreach ($new_value as $_attr_id => $_value_text) {
            $new_value_texts = array_filter(explode("\n", $_value_text));
            foreach ($new_value_texts as $new_value) {
                // if exist
                $check_exist = $this->getValueByTitle($_attr_id, $new_value);
                if($check_exist) {
                    $product_value_ids[] = $check_exist['id'];
                }else{
                    $try_create_id = $this->addValue($_attr_id, ['title' => $new_value]);
                    if($try_create_id) $product_value_ids[] = $try_create_id;
                }
            }
        }


        $objProductAttributeValueModel = new ProductAttributeValueModel(0);

        return $objProductAttributeValueModel->updateProductAttributes($product_id, $product_value_ids);

    }


    public function updateAttributeInSpecGroup($group_id, $attr_id, array $info) {
        $this->objProductAttributeModel->updateAttributeInSpecGroup($group_id, $attr_id, $info);
    }

    public function removeAttributeFromSpecGroup($group_id, $attr_id) {
        $this->objProductAttributeModel->removeAttributeFromSpecGroup($group_id, $attr_id);
    }

    public function addAttributeToSpecGroup($group_id, $attr_id, $ordering) {
        $this->objProductAttributeModel->addAttributeToSpecGroup($group_id, $attr_id, $ordering);
    }

    public function updateAttributeInCategory($cat_id, $attr_id, array $info) {
        $this->objProductAttributeModel->updateAttributeInCategory($cat_id, $attr_id, $info);
    }

    public function removeAttributeFromCategory($cat_id, $attr_id) {
        $this->objProductAttributeModel->removeAttributeFromCategory($cat_id, $attr_id);
    }

    public function addAttributeToCategory($cat_id, $attr_id, $ordering) {
        $this->objProductAttributeModel->addAttributeToCategory($cat_id, $attr_id, $ordering);
    }

    public function getAllAtributes() {
        return $this->objProductAttributeModel->getList(["numPerPage" => 2000]);
    }

    public function deleteValue($attr_id, $value_id) {
        $objProductAttributeValueModel = new ProductAttributeValueModel($attr_id);
        return $objProductAttributeValueModel->delete($value_id);
    }

    public function updateValue($attr_id, $value_id, array $info) {
        $objProductAttributeValueModel = new ProductAttributeValueModel($attr_id);
        return $objProductAttributeValueModel->updateFields($value_id, $info);
    }

    public function getValueByTitle($attr_id, $value_title) {
        $objProductAttributeValueModel = new ProductAttributeValueModel($attr_id);
        $filter_code = DataClean::makeInputSafe($value_title, DataType::ID);
        return $objProductAttributeValueModel->getInfoByCode($filter_code);
    }

    public function addValue($attr_id, array $info) {
        $objProductAttributeValueModel = new ProductAttributeValueModel($attr_id);
        return $objProductAttributeValueModel->create($info);
    }

    public function getListAttributeValues($attr_id) {
        $objProductAttributeValueModel = new ProductAttributeValueModel($attr_id);
        return $objProductAttributeValueModel->getList(['numPerPage' => 200]);
    }

    protected function deleteFileBeforeDeleteItem($item_id): bool
    {
        return true;
    }
}
