<?php

/**
 * Created by PhpStorm.
 * User: wpj
 * Date: 7/13/17
 * Time: 12:23 PM.
 */

namespace KupShop\ElninoBundle\GoogleTagManager;

trait ProductComputeTrait
{
    protected function getVariationValues($models)
    {
        $ret = [
            'prices' => [
                'min' => null,
                'max' => null,
            ],
            'codes' => [
                'min' => null,
                'max' => null,
            ],
            'size' => [],
            'ids' => [
                'min' => null,
                'max' => null,
            ],
        ];

        $allowed_params = ['ml', 'g'];

        foreach ($models as $model) {
            if ((!empty($model['campaign_codes']['O']) || !empty($model['campaign_codes']['V']) || $model['inStore'] <= 0) && count($models) > 1) {
                continue;
            }

            if (is_null($ret['prices']['min'])) {
                $ret['prices']['min'] = $model['price_array']['value_with_vat'];
                $ret['codes']['min'] = $model['code'];
                $ret['ids']['min'] = $model['id'];
            }

            if ($model['price_array']['price_with_vat']->lowerThan($ret['prices']['min'])) {
                $ret['prices']['min'] = roundPrice($model['price_array']['value_with_vat']);
                $ret['codes']['min'] = $model['code'];
                $ret['ids']['min'] = $model['id'];
            }

            if (!in_array($model['size'], $ret['size'])) {
                foreach ($allowed_params as $param) {
                    if (strpos($model['size'], $param) !== false) {
                        $ret['size'][] = $model['size'];
                    }
                }
            }
        }

        usort($ret['size'], ['KupShop\ElninoBundle\GoogleTagManager\ProductComputeTrait', 'sortByMl']);

        return $ret;
    }

    public static function sortByMl($a, $b)
    {
        return intval($a) > intval($b);
    }

    /**
     * Add amount of products by counter.
     */
    protected function addProducts($models)
    {
        foreach ($models as $model) {
            if ($this->getProductCounter() >= $this->getProductCounterMax()) {
                break;
            }

            $this->setProductData($model, [$model]);
            $this->incrementProductCounter();
        }
    }

    /**
     * Set IDs and total_values.
     *
     * @param $model \Product
     */
    protected function setProductData($model, $models)
    {
        $data = $this->dataLayer->getGtp();
        /** @var PricesCompute $pc */
        $pc = $this->pricesCompute;

        $values = $this->getVariationValues($models);

        $data->item_group_id[] = $model['model_old_id'];

        if (!empty($values['codes']['min'])) {
            $v = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($values['prices']['min'])));
            $data->dynx_totalvalue[] = $v;
            $data->ecomm_totalvalue[] = $v;

            $data->ecomm_prodid[] = $values['codes']['min'];
            $data->dynx_itemid[] = $values['codes']['min'];
        } else {
            if (!empty($model['code'])) {
                $data->ecomm_prodid[] = $model['code'];
                $data->dynx_itemid[] = $model['code'];
            }
        }

        $id_product = $values['ids']['min'];

        if (!empty($id_product) && !empty($models[$id_product])) {
            $product = $models[$id_product];
            $data->custom_converted_total_value_with_vat = $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($product['price_array']['value_with_vat'])));
            $data->custom_converted_total_value_without_vat = $pc->formatConvertedPrice($pc->convertConvertedPrice($product['price_array']['value_without_vat']));

            $data->custom_original_total_value_with_vat = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($product['price_array']['value_with_vat'])));
            $data->custom_original_total_value_without_vat = $pc->formatOriginalPrice($pc->convertOriginalPrice($product['price_array']['value_without_vat']));
        }
    }

    public function getVariationsData(&$variations)
    {
        $data = [];
        /** @var PricesCompute $pc */
        $pc = $this->pricesCompute;

        foreach ($variations as $id_variation => $variation) {
            $var = new \stdClass();

            /*
             * Novinka, Akce
             */
            $var->custom_news = $this->getCustomCampaign($variation, 'N');
            $var->custom_action = $this->getCustomCampaign($variation, 'A');

            $var->ecomm_prodid = $variation['code'];
            $var->dynx_itemid = $variation['code'];

            $var->ecomm_totalvalue = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($variation['price']['price_with_vat'])));
            $var->dynx_totalvalue = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($variation['price']['price_with_vat'])));

            $var->custom_converted_total_value_with_vat = $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($variation['price']['price_with_vat'])));
            $var->custom_converted_total_value_without_vat = $pc->formatConvertedPrice($pc->convertConvertedPrice($variation['price']['price_without_vat']));

            $var->custom_original_total_value_with_vat = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($variation['price']['price_with_vat'])));
            $var->custom_original_total_value_without_vat = $pc->formatOriginalPrice($pc->convertOriginalPrice($variation['price']['price_without_vat']));

            $data[$id_variation] = $var;
        }

        return json_encode($data, $this->dataLayer->getOutputOptions());
    }

    protected function getCustomCampaign($variation, $campaign)
    {
        return isset($variation['campaign_codes'][$campaign]) ? '1' : '0';
    }

    /**
     * Zvolí jestli se jedná o kosmetiku / parfémy.
     */
    protected function getCustomSection($returnNav)
    {
        foreach ($returnNav as $item) {
            if (strpos(strtolower($item['text']), strtolower(translate('perfumes_category', 'elnino'))) !== false) {
                return 'parfemy';
            }
        }

        return 'kosmetika';
    }

    /**
     * Pohlaví.
     */
    protected function getCustomSex($param)
    {
        if (isset($param['values'][16])) {
            return 'm';
        }

        if (isset($param['values'][17])) {
            return 'w';
        }

        if (isset($param['values'][22])) {
            return 'u';
        }

        if (isset($param['values'][23])) {
            return 'd';
        }
    }
}
