<?php

namespace PortalPeople\gforce\models\products;
use yii;
use yii\helpers\Json;

class Gforce extends \common\models\DynamicModel
{

    public static function segment_title()
    {
        return 'GForce Attributes';
    }
    /**
     * Undocumented function
     *
     *    'colour' => [
     *        'rules' => [
     *            'string'
     *        ],
     *        'label' => Yii::t('app', "Colour",),
     *        'input' => 'default',
     *     ],
     * @return void
     */
    public static function localattributes()
    {
        return [
            'erpid' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "ERP ID",),
                'input' => 'default',
            ],
            'stock_level' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Stock Level",),
                'input' => 'default',
            ],
            'stock_reminder' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Stock Reminder Level",),
                'input' => 'default',
            ],
            'haulierid' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Haulier ID",),
                'input' => 'default',
            ],
            
        ];
    }


    public function dataLoad($modelData) {
        /*if ($ext_data !== null) {
            empty($this->erpid) && array_key_exists('erpid', $ext_data) ? ( $this->erpid = $ext_data['erpid'] ? $ext_data['erpid'] : $this->erpid) : false;
            empty($this->erp_name) && array_key_exists('erp_name', $ext_data) ? ( $this->erp_name = $ext_data['erp_name'] ? $ext_data['erp_name'] : $this->erp_name) : false;
            empty($this->erp_qty) && array_key_exists('erp_qty', $ext_data) ? ( $this->erp_qty = $ext_data['erp_qty'] ? $ext_data['erp_qty'] : $this->erp_qty) : false;
            empty($this->erp_discountid) && array_key_exists('erpdiscountid', $ext_data) ? ( $this->erp_discountid = $ext_data['erpdiscountid'] ? $ext_data['erpdiscountid'] : $this->erp_discountid) : false;
            empty($this->erp_link) && array_key_exists('erp_link', $ext_data) ? ( $this->erp_link = $ext_data['erp_link'] ? $ext_data['erp_link'] : $this->erp_link) : false;
            empty($this->erp_cost) && array_key_exists('erp_cost', $ext_data) ? ( $this->erp_cost = $ext_data['erp_cost'] ? $ext_data['erp_cost'] : $this->erp_cost) : false;
            empty($this->unit) && array_key_exists('unit', $ext_data) ? ( $this->unit = $ext_data['unit'] ? $ext_data['unit'] : $this->unit) : false;
            empty($this->uom_name) && array_key_exists('uom_name', $ext_data) ? ( $this->uom_name = $ext_data['uom_name'] ? $ext_data['uom_name'] : $this->uom_name) : false;
            empty($this->haulierid) && array_key_exists('haulierid', $ext_data) ? ( $this->haulierid = $ext_data['haulierid'] ? $ext_data['haulierid'] : $this->haulierid) : false;
            empty($this->supplierid) && array_key_exists('supplierid', $ext_data) ? ( $this->supplierid = $ext_data['supplierid'] ? $ext_data['supplierid'] : $this->supplierid) : false;
        }*/
    }

    public function adjustStockValueFromCartLine(
        $cartlineQty,
        $product = null,
        $haulier = null,
        ?array &$cartlineData = null
    ) {
        $calculatorClass = $this->resolveStockCalculatorClass();
        $calculator = new $calculatorClass();
        $adjustmentLedger = $this->extractAdjustmentLedger($cartlineData);
        $newStock = $calculator->decreaseStockValue(
            $this,
            $cartlineQty,
            $product,
            $haulier,
            ['erpid', 'stock_code', 'sku', 'id'],
            ['haulierid', 'haulier_id', 'id'],
            $adjustmentLedger,
            $calculator->buildAdjustmentKey('decrease', $this, $product, $haulier)
        );

        $this->stock_level = (string) $newStock;
        $this->persistAdjustmentLedger($cartlineData, $adjustmentLedger);

        return $newStock;
    }

    public function increaseStockValueFromCartLine(
        $cartlineQty,
        $product = null,
        $haulier = null,
        ?array &$cartlineData = null
    ) {
        $calculatorClass = $this->resolveStockCalculatorClass();
        $calculator = new $calculatorClass();
        $adjustmentLedger = $this->extractAdjustmentLedger($cartlineData);
        $newStock = $calculator->increaseStockValue(
            $this,
            $cartlineQty,
            $product,
            $haulier,
            ['erpid', 'stock_code', 'sku', 'id'],
            ['haulierid', 'haulier_id', 'id'],
            $adjustmentLedger,
            $calculator->buildAdjustmentKey('increase', $this, $product, $haulier)
        );

        $this->stock_level = (string) $newStock;
        $this->persistAdjustmentLedger($cartlineData, $adjustmentLedger);

        return $newStock;
    }

    private function extractAdjustmentLedger(?array $cartlineData): array
    {
        if ($cartlineData === null) {
            return [];
        }

        if (!isset($cartlineData['stock_adjustments']) || !is_array($cartlineData['stock_adjustments'])) {
            $cartlineData['stock_adjustments'] = [];
        }

        return $cartlineData['stock_adjustments'];
    }

    private function persistAdjustmentLedger(?array &$cartlineData, array $adjustmentLedger): void
    {
        if ($cartlineData === null) {
            return;
        }

        $cartlineData['stock_adjustments'] = $adjustmentLedger;
    }

    private function resolveStockCalculatorClass(): string
    {
        $newClass = '\\PortalPeople\\delivery\\components\\StockAdjustmentCalculator';
        if (class_exists($newClass)) {
            return $newClass;
        }

        throw new \RuntimeException(
            'Missing required package portalpeople/ngauge-delivery: '
            . 'PortalPeople\\delivery\\components\\StockAdjustmentCalculator not found.'
        );
    }
}
