<?php

namespace Modules\PrintSuppliers\Controllers;

use Illuminate\Http\Request;
use DB;

class MerchizeTemplate extends Controller
{

    public function getTemplates (&$items, &$order) {
        $retval = [];

        foreach ($items as $key => $input) {
            $input['template'] = new MerchizeTemplate;
            $output = $input['template']->getTemplate($input);
            if ($output) {
                $retval[] = $output;
            } else {
                throw new \Exception("Can not find merchize template for $input[id]", 10);
            }
        }

        return $retval;
    }

    public function template (Request $request) {

        $validator = \Validator::make($request->all(), [
            'category_id' => 'required',
            'color' => 'required',
            'size' => 'required',
            // 'type' => 'required',
            // 'style' => 'required',
            'position' => 'required',
        ]);

        if ($validator->fails())
        {
            return $validator->messages();
        }

        $input = [
            $request->input('category_id'),
            $request->input('color'),
            $request->input('size'),
            $request->input('type'),
            $request->input('style'),
            $request->input('position'),
        ];

        $template = $this->getTemplate($input);
        if ($template) {
            return [
                'status' => 'success',
                'template' => $template
            ];
        } else {
            return [
                'status' => 'fail'
            ];
        }
    }

    public function getTemplate ($input) {
        $this->style = $input[4];
        $this->size = $this->parseSize($input[2]);
        $this->color = $this->parseColor($input[1]);
        $this->product = $this->parseProduct($input[0]);
        $this->type = $input[3];
        $retval = null;
        if ($this->product
            && $this->color
            && $this->size
            && in_array($this->style, [null, 'Classic T-shirt'])
            && $this->type == 'Women') {
            $retval = [
                'product' => $this->product,
                'Color' => ucwords($this->color),
                'Size' => $this->size,

            ];
            $retval = $this->buildAttribute($retval);
        }
        return $retval;
    }

    public function buildAttribute($data) {
        $retval['attributes'] = [];
        foreach ($data as $key => $value) {
            $item = [];
            $item['name'] = $key;
            $item['option'] = $value;
            array_push($retval['attributes'], $item);
        }
        $retval['resolution']  = '4200x4800';
        return $retval;
    }

    public function parseProduct($cateID) {
        switch ($cateID) {
            case 7:
                return 'T500LPF-Women T-shirt 2D (Made In US)';
                break;
            case 780:
                return 'T500LPF-Women T-shirt 2D (Made In US)';
                break;
            default:
                return 'T500LPF-Women T-shirt 2D (Made In US)';
                break;
        }
    }

    public function parseSize ($size) {
        switch ($size) {
            default:
                $size = $size;
                break;
        }
        return $size;
    }

    public function parseColor($color) {
        switch ($color) {
            case 'Sport grey':
            case 'Ash':
            case 'Heather':
                $retval = 'Heather Grey';
                break;
            case 'Royal':
            case 'Blue':
                $retval = 'Royal Blue';
                break;
            case 'Charcoal Heather':
                $retval = 'Dark Heather';
                break;
            case 'Maroon':
                $retval = 'Cardinal';
                break;
            case 'Soft Pink':
                $retval = 'Light Pink';
                break;
            case 'Dark Navy':
                $retval = 'Navy';
                break;
            default:
                $retval = $color;
                break;
        }
        $retval = ucwords($retval);
        if (in_array($retval, ['Black', 'White', 'Dark Heather', 'Heather Grey', 'Light Blue', 'Light Pink', 'Navy', 'Red', 'Royal Blue', 'Purple', 'Irish Green', 'Cardinal', 'Charcoal', 'Daisy'])) {
            return $retval;
        }
    }
}
