<?php

namespace Webkul\WebkulCsvBundle\Classes;

class ProductCustomType
{
    /** @var array */
    public $customProductTypeHeaders = [
        'bigCommerceItemTypeHeader' => 'Item Type',
        'bigCommerceProductTypeHeader' => 'Product Type',
        'MagentoProductTypeHeader' => 'product_type'
    ];

    /** @var array */
    protected $magentoProductType = [
        'simpleProduct' => 'simple',
        'modelProduct' => 'configurable'
    ];

    /** @var array */
    protected $bigCommerceItemType = [
        'product' => 'Product',
        'variantProduct' => 'SKU',
    ];

    /** @var array */
    protected $bigCommerceProductType = [
        'product' => 'P'
    ];

    /**
     * Format Magento Product Type
     * @param $akeneoProductType
     * @return string
     */
    protected function magentoProductType($akeneoProductType)
    {
        if (isset($this->magentoProductType[$akeneoProductType])) {
            return $this->magentoProductType[$akeneoProductType];
        }
        
        return 'simple';
    }
    
    /**
     * Format BigCommerce Product Type
     * @param $akeneoProductType
     * @return string
     */
    protected function bigCommerceProductType($akeneoProductType)
    {
        if (isset($this->bigCommerceProductType[$akeneoProductType])) {
            return $this->bigCommerceProductType[$akeneoProductType];
        }
        
        return 'P';
    }

    /**
     * Format BigCommerce Product Type
     * @param $akeneoProductType
     * @return string
     */
    protected function bigCommerceItemType($akeneoProductType)
    {
        if (isset($this->bigCommerceItemType[$akeneoProductType])) {
            return $this->bigCommerceItemType[$akeneoProductType];
        }
        
        return 'Product';
    }
   
    /**
     * Format product type
     * @param $code
     * @return string
     */
    public function formatProductType(string $head, object $productCustomType, array $item)
    {
        $productType = '';

        if ($head === 'product_type') {
            if ($item['parent'] || isset($item['identifier'])) {
                $productType = $this->magentoProductType('simpleProduct');
            } else {
                $productType = $this->magentoProductType('modelProduct');
            }
        } elseif ($head === 'Product Type') {
            if (!$item['parent'] || isset($item['code'])) {
                $productType = $this->bigCommerceProductType('product');
            }
        } elseif ($head === 'Item Type') {
            if (!$item['parent'] || isset($item['code'])) {
                $productType = $this->bigCommerceItemType('product');
            } else {
                $productType = $this->bigCommerceItemType('variantProduct');
            }
        }

        return $productType;
    }
}
