<?php
namespace PortalPeople\magento\models;

use yii;

class Products extends \common\models\DynamicModel
{

    public static function segment_title()
    {
        return 'Magento Attributes';
    }


    public static function localattributes(){
        $localattributes = [
            'id' => [
                'rules' => [
                    ['integer'],
                ],
                'label' => Yii::t('app', "ID"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => false,
                    'fieldlist' => '',
                    'options' => [
                    ],
                    'width' => 'col-md-3',
                ],
            ],
            'sku' => [
                'rules' => [
                    ['string', 'max' => 255],
                ],
                'label' => Yii::t('app', "SKU"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => false,
                    'fieldlist' => '',
                    'options' => [
                    ],
                    'width' => 'col-md-3',
                ],
            ],
            'name' => [
                'rules' => [
                    ['string', 'max' => 255],
                ],
                'label' => Yii::t('app', "Name"),
                'input' => [
                    'input' => 'textInput',
                    'fieldclass' => false,
                    'fieldlist' => '',
                    'options' => [
                    ],
                    'width' => 'col-md-3',
                ],
            ],
        ];

        return  $localattributes;
    }


    /**
     * Process the attributes of the model by converting values to boolean
     *
     * If the value of an attribute is 'Y' or 'N', it is converted to boolean true
     * or false. Otherwise, the attribute value remains unchanged.
     */
    public function process(){
        
    }

    public function dataLoad($modelData) {
        $product = $modelData;
        $ext_data = json_decode($modelData->data, true);
        $data = $ext_data['magento'] ?? null;
        
        // Populate from stored ext_data first
        if ($data !== null) {
            $this->load($data, '');
        }

        if (!Yii::$app->request->isConsoleRequest && Yii::$app->request->isGet && Yii::$app->controller->action->id === 'update' && Yii::$app->request->get('id') == $modelData->id) {
            $client = Yii::$app->magentoClient; // however it's registered
            // Single product
            $Mageproduct = Yii::$app->cache->getorset('magento_product_' . $product->stock_code, function() use ($client, $product) {
                return $client->getProduct($product->stock_code);
            }, 3600); // Cache for 1 hour
            Yii::warning($Mageproduct, 'magento');
            if(empty($this->name)) {
                $this->name = $Mageproduct['name'];
            }
            if(empty($this->sku)) {
                $this->sku = $Mageproduct['sku'];
            }
            if(empty($this->id)) {
                $this->id = $Mageproduct['id'];
            }
        }

        $decoded = is_array($modelData->data) ? $modelData->data : (json_decode($modelData->data, true) ?? []);
        $decoded['magento'] = $this;
        $modelData->data = json_encode($decoded);
    }
}
