<?php

namespace Modules\PrintSuppliers\Controllers;

use Illuminate\Http\Request;
use Modules\PrintSuppliers\Models\PrintProvider;
use Modules\PrintSuppliers\Models\Blueprint;
use DB;

class PrintifyTemplateV2 extends Controller
{

    public function getTemplates ($items, &$order) {
        $retval = [];
        $allBluePrintId = [];
        $listColorSwiftExpress = [
            'colors' => [
                'black', 'white', 'sport grey',
                'schwarz', 'blanc', 'weiß', 'grau meliert', 'graumeliert', 'heather grey', 'sport grau',
                'noir', 'blanc', 'gris sport'
            ],
            'sizes' => ['s', 'm', 'l', 'xl', '2xl', '3xl']
        ];
        $checkAllowBigSize = false;
        if ($order->shipping_type != 'standard') {
            $checkAllowBigSize = true;
            foreach ($items as $key => $input) {
                $size = $this->parseSize($input[1]);
                if ($input[3] == 'Men' && $input[4] == 'Classic T-shirt' && $input[5] == 'front' && (!in_array(strtolower($input[1]), ['black', 'white', 'navy', 'dark grey heather', 'athletic heather']) || !in_array(strtolower($size), ['s', 'm', 'l', 'xl', '2xl', '3xl']))) {
                    $checkAllowBigSize = false;
                    break;
                }
            }

        }
        foreach ($items as $key => $input) {

            $input['template'] = new PrintifyTemplateV2;
            $input['template']->category_id = $input[0];
            $input['template']->color = $input[1];
            $input['template']->queryColor = $input['template']->parseColor($input[1]);
            $input['template']->size = $input['template']->parseSize($input[2]);
            $input['template']->type = $input[3];
            $input['template']->style = $input[4];
            $input['template']->position = $input[5];
            $input['template']->shipping_type = $order->shipping_type;
            $input['template']->checkAllowBigSize = $checkAllowBigSize;
            $input['template']->blueprint_id = $input['template']->getBluePrint();

            if (!$input['template']->blueprint_id) {
                \Log::info($items[$key]);
                \Log::warning($input);
                throw new \Exception("Can not detect blueprint_id for item : $input[id]", 10);
            }
            $input['template']->addTriBlendColor();

            $queryPrintProviders = DB::table('print_suppliers_variant')
                ->where('size',$input['template']->size )
                ->whereIn('color', $input['template']->queryColor)
                ->where('blueprint_id', $input['template']->blueprint_id)
                ->orderBy('cost', 'asc')
                ->where('status', 'in-stock');
            if (!empty($order->force_print_provider_ids)) {
                $queryPrintProviders->whereIn('print_provider_id', $order->force_print_provider_ids); // áo trademark chỉ đi Dimona Tee, Fulfill Engine, Maco Fine Art, Underground-threads
            }
            if ($input[5] != 'front' && $input['template']->blueprint_id == 77) {
                $queryPrintProviders->where('print_provider_id', '=', 61); // hoodie mặt sau hoặc 2 mặt chỉ đi Dimona Tee
            }
//            if (!empty($order->only_women_classic_tshirt)) {
//                $queryPrintProviders->where('print_provider_id', '=', 39); // chỉ có women classic t shirt thì đi SwiftPOD
//            }
            if (!empty($order->has_women_classic_tshirt)) {
                $queryPrintProviders->whereIn('print_provider_id', [39]); // có women classic t shirt thì đi SwiftPOD
            }
            if ($input['template']->blueprint_id == 49 && $order->shipping_type == 'standard') {
                $queryPrintProviders->where('print_provider_id', '=', 99); // classic sweatshirt chỉ đi Print Choice
            }
            if ($order->shipping_type != 'standard') {
                $queryPrintProviders->where('print_provider_id', '!=', 99); // Không đi printify choice với đơn express premium
            }

            if ($order->shipping_type != 'standard' && $input['template']->blueprint_id == 6 && array_intersect(array_map('strtolower', $input['template']->queryColor), $listColorSwiftExpress['colors']) && in_array(strtolower($input['template']->size), $listColorSwiftExpress['sizes'])) {
                $queryPrintProviders->where('print_provider_id', '=', 39); // express hoặc premium, heavy - size: S-3XL - color: black, white, sport grey thì đi swiftpod
            }
            $input['print_providers'] = $queryPrintProviders->pluck('print_provider_id')->toArray();
            if (!count($input['print_providers'])) {
                \Log::info($items[$key]);
                \Log::warning($input);
                throw new \Exception("No print providers for item : " . json_encode($input) , 11);
            }
            $allBluePrintId[] = $input['template']->blueprint_id;
            $inputs[$key] = $input;
        }

        $availablePrintProviders = DB::table('print_suppliers_provider')->where('location', 'like', '%country":"US"%')->pluck('id')->toArray();
        foreach ($inputs as $key => $value) {
            $availablePrintProviders = array_intersect($availablePrintProviders, $inputs[$key]['print_providers']);
        }
        if (in_array(706, $allBluePrintId)) { // Unisex Garment-Dyed T-shirt
            $availablePrintProviders = [99]; //  Printify Choice
        }
        if (in_array(99, $availablePrintProviders) && $order->shipping_type == 'standard') {
            $availablePrintProviders = [99]; // Printify Choice

        }
        if (in_array(29, $availablePrintProviders) && $order->shipping_type == 'standard') {
            $availablePrintProviders = [29]; // Monster Digital
        }
        $prices = [];
        foreach ($availablePrintProviders as $key => $printProvider) {
            $prices[$printProvider] = $this->calculatePrice($inputs, $printProvider);
        }

        asort($prices);
        reset($prices);
        $bestPrintProvider = key($prices);

        if (!$bestPrintProvider) {
            throw new \Exception("Can not get best print providers", 99);
        }

        foreach ($inputs as $key => $input) {
            $variant = DB::table('print_suppliers_variant')
                ->where('size',$input['template']->size )
                ->whereIn('color', $input['template']->queryColor)
                ->where('blueprint_id', $input['template']->blueprint_id)
                ->orderBy('cost', 'asc')
                ->where('status', 'in-stock')
                ->where('print_provider_id', $bestPrintProvider)
                ->select(['blueprint_id', 'print_provider_id', 'variant_id', 'placeholders'])
                ->first();
            $placeholders = json_decode($variant->placeholders);
            $resolution = $placeholders[0]->width . 'x' . $placeholders[0]->height;
            unset($variant->placeholders);
            foreach ($placeholders as $key => $placeholder) {
                if ($input['template']->position == $placeholder->position) {
                    $resolution = $placeholder->width . 'x' . $placeholder->height;
                }
            }
            if ($variant->blueprint_id == 564) {
                $resolution = '1571x1571'; // Max resolution for square
            }
            $variant->resolution = $resolution;
            if (!empty($order->attributes['force_print_provider_id'])) {
                $variant->print_provider_id = $order->attributes['force_print_provider_id'];
            }
            if ($order->code == 'US0972043') {
                $variant->print_provider_id = 61;
            }
            $variant->is_express_shipping_type = $this->checkIsExpressShippingType($input);
            $retval[] = $variant;
        }

        $order->cost = $prices[$bestPrintProvider];
        $order->print_provider = $bestPrintProvider;
        if (!empty($order->attributes['force_print_provider_id'])) {
            $order->print_provider = $order->attributes['force_print_provider_id'];
        }
        if ($order->code == 'US0972043') {
            $order->print_provider = 61;
        }
        $order->availablePrintProviders = $prices;

        return $retval;
    }

    private function checkIsExpressShippingType ($input) {
        $retVal = 0;
        $checkBlueprint = in_array($input['template']->blueprint_id, [6, 12]);
        $checkSize = in_array(strtolower($input['template']->size), ['s', 'm', 'l', 'xl', '2xl', '3xl']);
        $checkShippingType = $input['template']->shipping_type != 'standard';
        $checkColor = false;
        if ($input['template']->blueprint_id == 12 && in_array(strtolower($input['template']->color), ['black', 'white', 'navy', 'dark grey heather', 'athletic heather'])) {
            $checkColor = true;
        } else if ($input['template']->blueprint_id == 6 && in_array(strtolower($input['template']->color), ['black', 'white', 'navy', 'sport grey'])) {
            $checkColor = true;
        }
        if ($checkBlueprint && $checkSize && $checkShippingType && $checkColor) {
            $retVal = 1;
        }
        return $retVal;

    }

    public function calculatePrice ($inputs, $printProvider) {
        $price = 0;
        $ships = [];

        // dd($inputs);

        foreach ($inputs as $key => $input) {
            $q = DB::table('print_suppliers_variant')
                ->where('size',$input['template']->size )
                ->whereIn('color', $input['template']->queryColor)
                ->where('blueprint_id', $input['template']->blueprint_id)
                ->orderBy('cost', 'asc')
                ->where('status', 'in-stock')
                ->where('print_provider_id', $printProvider)
                ->first();
            if ($input[0] == 780 ) { 
                $costs = json_decode($q->costs);
                if (isset($costs[0])) {
                    $cost = $costs[0];
                    $q->cost += round($cost->printing *  80 / 67);
                }
            }
            $price += $q->cost * $input['quantity'];
            $inputs['price'] = $q->cost;
            if (!isset($ships[$input['template']->blueprint_id])) {
                $ships[$input['template']->blueprint_id] = 0;
            }
            $ships[$input['template']->blueprint_id] += $input['quantity'];
        }

        // dd($ships);

        $shipCost = 0;

        foreach ($ships as $blueprint => $quantity) {
            $row = DB::table('print_suppliers_printify_shipping')->where([
                'blueprint' => $blueprint,
                'print_provider' => $printProvider
            ])->first();
            if ($row) {
                $shipCost += ($row->first_item + $row->additional_items * ($quantity - 1));
            } else {
                throw new \Exception("Khong tinh duoc phi ship", 40);

            }
        }

        $price += $shipCost;

        return $price;
    }

    public function fetchPrintProvider ($printProviderId) {
        $printProvider = PrintProvider::firstOrNew(['id'=> $printProviderId]);
        $response = $this->sendRequest("https://api.printify.com/v1/catalog/print_providers/$printProviderId.json");
        $printProvider->update($response);
        $printProvider->save();
        foreach ($response['blueprints'] as $key => $value) {

            $blueprint = Blueprint::firstOrNew(['id' => $value['id']]);
            $blueprint->update($value);
            $blueprint->save();
            $this->fetchPrintProviderVariant($printProviderId, $value['id']);
        }

        return $printProvider;
    }

    public function fetchPrintProviderVariant ($printProviderId, $blueprintId) {
        $response = $this->sendRequest("https://api.printify.com/v1/catalog/blueprints/$blueprintId/print_providers/$printProviderId/variants.json");
        $variants  = [];
        if ($response && isset($response['variants'])) {
            foreach ($response['variants'] as $variant) {
                $variantId = $variant['id'];
                $variants[$variantId] = $variant;
            }
        }

        $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NjQzNzM3MTEsImRhdGEiOnsidXNlcl9pZCI6OTQ0MTUzNX19.UrmTAfUJ44FlBq89mt2foQP_2xsgL_cmB19WnAGA6BE';
        $response = $this->sendRequest("https://printify.com/api/v1/blueprints/$blueprintId/$printProviderId", $token);

        if ($response && isset($response['print_provider']) && isset($response['print_provider']['variants'])) {
            foreach ($response['print_provider']['variants'] as $variant) {
                $variantId = $variant['id'];
                $variants[$variantId]['status'] = $variant['status'] && $variant['status'] ? $variant['status'] : 'out-of-stock';
                $variants[$variantId]['costs'] = $variant['costs'];
            }
        } else {
            return;
        }

        foreach ($variants as $key => $variant) {

            if (!isset($variant['id'])) {
                continue;
            }

            if (isset($variant['options']['color'])) {
                $color = $variant['options']['color'];
            } else {
                $color = "";
            }

            if (isset($variant['options']['size'])) {
                $size = $variant['options']['size'];
            } else {
                $size = "";
            }

            $row = DB::table('print_suppliers_variant')->where([
                'blueprint_id' => $blueprintId,
                'variant_id' => $variant['id'],
                'print_provider_id' => $printProviderId,
            ])->first();
            if (!$row) {
                DB::table('print_suppliers_variant')->insert([
                    'variant_id' => $variant['id'],
                    'blueprint_id' => $blueprintId,
                    'print_provider_id' => $printProviderId,
                    'color' => $color,
                    'size' => $size,
                    'status' => $variant['status'],
                    'costs' => json_encode($variant['costs']),
                    'cost' => $variant['costs'][0]['result'],
                    'placeholders' => json_encode($variant['placeholders']),
                    'update' => true,
                    'created_at' => date('Y-m-d H:i:s', time()),
                    'updated_at' => date('Y-m-d H:i:s', time()),
                ]);
            } else {
                DB::table('print_suppliers_variant')->where([
                    'blueprint_id' => $blueprintId,
                    'variant_id' => $variant['id'],
                    'print_provider_id' => $printProviderId,
                ])->update([
                    'variant_id' => $variant['id'],
                    'blueprint_id' => $blueprintId,
                    'print_provider_id' => $printProviderId,
                    'color' => $color,
                    'size' => $size,
                    'status' => $variant['status'],
                    'costs' => json_encode($variant['costs']),
                    'cost' => $variant['costs'][0]['result'],
                    'placeholders' => json_encode($variant['placeholders']),
                    'update' => true,
                    'updated_at' => date('Y-m-d H:i:s', time()),
                ]);
            }

                // die;
        }

        DB::table('print_suppliers_variant')->where([
            'blueprint_id' => $blueprintId,
            'print_provider_id' => $printProviderId,
            'update' => false
        ])->delete();
        DB::table('print_suppliers_variant')->where([
            'blueprint_id' => $blueprintId,
            'print_provider_id' => $printProviderId,
        ])->update([
            'update' => false,
            'updated_at' => date('Y-m-d H:i:s', time()),
        ]);

    }

    public function getTemplate ($input) {

        $retval = [
            null, null, null, null
        ];
        $this->blueprint_id = null;
        $print_provider_id = null;
        $variant_id = null;
        $this->category_id = $input[0];
        $this->color = $input[1];
        $this->queryColor = $this->parseColor($input[1]);
        $this->size = $this->parseSize($input[2]);
        $this->type = $input[3];
        $this->style = $input[4];
        $this->position = $input[5];
        $this->blueprint_id = $this->getBluePrint();
        $this->addTriBlendColor();
        $variant = null;

        // 29 Monster
        // 61 Dimona Tee
        // 45 Awkward
        // 3 Dream Junction
        // 39 Swift pod
        // 16 MyLocker
        // 52 Stakes Manufacturing

        foreach ([29, 61, 45, 3, 39, 16, 52] as  $print_provider_id) {
            if ($this->blueprint_id == 157 && $print_provider_id == 29) {
                continue;
            }
            if (!$variant) {
                $query = DB::table('print_suppliers_variant')
                    ->where('size',$this->size )
                    ->whereIn('color', $this->queryColor)
                    ->where('blueprint_id', $this->blueprint_id)
                    ->where('print_provider_id', $print_provider_id);
                $variant = $query->first();
            }
        }

        $resolution = null;

        if ($variant) {
            $variant_id = $variant->variant_id;
            $print_provider_id = $variant->print_provider_id;
            $placeholders = json_decode($variant->placeholders);
            $resolution = $placeholders[0]->width . 'x' . $placeholders[0]->height;
            foreach ($placeholders as $key => $placeholder) {
                if ($this->position == $placeholder->position) {
                    $resolution = $placeholder->width . 'x' . $placeholder->height;
                }
            }
        } else {
            $print_provider_id = null;
        }

        return [
            "$this->blueprint_id",
            "$print_provider_id",
            "$variant_id",
            "$resolution"
        ];
    }

    public function parseSize ($size) {
        switch ($size) {
            case 'Large':
                $size = 'L';
                break;
            case 'Unisex Large':
                $size = 'L';
                break;
            case 'Unisex 2XL':
                $size = '2XL';
                break;
            case 'Unisex XL':
                $size = 'XL';
                break;
            case 'Unisex Medium':
                $size = 'M';
                break;
            case 'Unisex Small':
                $size = 'S';
                break;
            case 'Medium':
                $size = 'M';
                break;
            case 'Small':
                $size = 'S';
                break;
            default:
                $size = str_replace('" x', '" ×', $size);
                break;
        }
        return $size;
    }

    public function getBluePrint()
    {
        switch ($this->category_id) {
            case 8:
                // $this->blueprint_id = 77;
                break;
            case 54:
                // $this->blueprint_id = 49;
                break;
            case 151:
                // $this->blueprint_id = 66;
                break;
            case 181:
                // $this->blueprint_id = 66;
                break;
            case 48:
                // $this->blueprint_id = 39;
                if ($this->type == 'Women') {
                    // $this->blueprint_id = 18;
                }
                break;
            case 68:
                // $this->blueprint_id = 80;
                break;
            case 180:
                // $this->blueprint_id = 77;
                break;
            case 59:
                $this->blueprint_id = 147;
                if ($this->type == 'Women') {
                    $this->blueprint_id = 5;
                }
                break;
            case 74:
//                $this->blueprint_id = 33;
                break;
            case 164:
//                $this->blueprint_id = 603;
                break;
            default:
                break;
        }
        $this->getBluePrintByStyle();
        return $this->blueprint_id;
    }

    public function getBluePrintByStyle () {
        switch ($this->type) {
            case 'Women':
                if ($this->style == 'V-neck T-shirt') {
                    $this->blueprint_id = 48;
                }
                if ($this->style == 'Ringer T-shirt') {
                    $this->blueprint_id = 412;
                }
                if ($this->style == 'Crewneck Sweatshirt') {
                    // $this->blueprint_id = 49;
                }
                if ($this->style == 'Premium Sweatshirt') {
                    $this->blueprint_id = 446;
                }
                if ($this->style == 'Premium Hoodie') {
                    $this->blueprint_id = 439;
                }
                if ($this->style == 'Tri-blend T-shirt') {
                    $this->blueprint_id = 460;
                }
                if ($this->style == 'Tri-Blend T-Shirt (Extra Soft)') {
                    $this->blueprint_id = 460;
                }
                if ($this->style == 'Classic Hoodie') {
                    // $this->blueprint_id = 77;
                }
                if ($this->style == 'Classic Long Sleeves') {
                    // $this->blueprint_id = 80;
                }
                if ($this->style == 'Premium Long Sleeves') {
                    $this->blueprint_id = 41;
                }
                if ($this->style == 'Classic RacerBack') {
                    $this->blueprint_id = 18;
                }
                if ($this->style == 'Tri-Blend Racerback (Extra Soft)') {
                    $this->blueprint_id = 141;
                }
                if ($this->style == 'Classic T-shirt') {
                    $this->blueprint_id = 466;
                }
                break;
            case 'Men':
            case 'Unisex':
                if ($this->style == 'Heavyweight T-shirt') {
                    $this->blueprint_id = 6;
                }
                if ($this->style == 'V-neck T-shirt') {
                    $this->blueprint_id = 48;
                }
                if ($this->style == 'Ringer T-shirt') {
                    $this->blueprint_id = 412;
                }
                if ($this->style == 'Classic T-shirt') {
                    $this->blueprint_id = 36;
                }
                if ($this->style == 'Tri-blend T-shirt') {
                    $this->blueprint_id = 139;
                }
                if ($this->style == 'Tri-Blend T-Shirt (Extra Soft)') {
                    $this->blueprint_id = 139;
                }
                if ($this->style == 'Crewneck Sweatshirt') {
                     $this->blueprint_id = 49;
                }
                if ($this->style == 'Premium Sweatshirt') {
                    $this->blueprint_id = 446;
                }
                if ($this->style == 'Premium Hoodie') {
                    $this->blueprint_id = 439;
                }
                if ($this->style == 'Classic Hoodie') {
                     $this->blueprint_id = 77;
                }
                if ($this->style == 'Classic Long Sleeves') {
                    // $this->blueprint_id = 80;
                }
                if ($this->style == 'Premium Long Sleeves') {
                    $this->blueprint_id = 41;
                }

                if ($this->style == 'Classic Tank') {
                    // $this->blueprint_id = 39;
                }
                if ($this->style == 'Premium Tank') {
                    $this->blueprint_id = 880;
                }
                if ($this->style == 'Hoodie') {
                    $this->blueprint_id = 77;
                }
                if ($this->style == 'Comfort Colors Tee' && $this->category_id == 7) {
                    $this->blueprint_id = 706;
                }
                if ($this->style == 'Classic Zip Hoodie') {
                    $this->blueprint_id = 66;
                }
                break;
            case 'Youth':
                if ($this->style == 'Kids Pullover Hoodie') {
                    $this->blueprint_id = 314;
                }
                if ($this->style == 'Classic Hoodie') {
                    $this->blueprint_id = 314;
                }
                if ($this->style == 'Crewneck Sweatshirt') {
                    $this->blueprint_id = 1256;
                }
                if ($this->style == 'Heavyweight T-shirt') {
                    $this->blueprint_id = 157;
                }
                break;
            case 'Kids':
//                if ($this->style == 'Kids Softstyle Tee') {
//                    $this->blueprint_id = 81;
//                }
                if ($this->style == 'Kids Long Sleeve') {
                    $this->blueprint_id = 586;
                }
                if ($this->style == 'Kids Pullover Hoodie') {
                    $this->blueprint_id = 603;
                }
                if ($this->style == 'Baby Onesie') {
                    $this->blueprint_id = 33;
                }
                break;
            default:
                break;
        }
        if (!empty($this->checkAllowBigSize) && !empty($this->shipping_type) && $this->shipping_type != 'standard' && $this->type == 'Men' && $this->style == 'Classic T-shirt' && !empty($this->position) && $this->position == 'front') {
            if (!empty($this->color) && in_array(strtolower($this->color), ['black', 'white', 'navy', 'dark grey heather', 'athletic heather']) && !empty($this->size) && in_array(strtolower($this->size), ['s', 'm', 'l', 'xl', '2xl', '3xl'])) {
                $this->blueprint_id = 12;
            }
        }
        if ($this->style == 'Short Sleeve Onesie') {
            $this->blueprint_id = 33;
        }

    }

    public function addTriBlendColor() {

        if (in_array($this->blueprint_id, [139, 460])) {
            $this->queryColor[] = 'Tri-Blend ' . $this->color;
            $this->queryColor[] = 'Tri-Blend Vintage ' . $this->color;
            $this->queryColor[] = 'Tri-Blend Heather ' . $this->color;
            if (strpos($this->color, 'Vintage') !== false) {
                $this->queryColor[] = str_replace('Vintage ', '', $this->color);
            }
            if (strpos($this->color, ' Heather') !== false) {
                $this->queryColor[] = str_replace(' Heather', '', $this->color);
            }
        }
    }

    public function parseColor($color) {
        $retval = [$color];
        switch ($color) {
            case 'Royal blue':
            case 'Royal':
            case 'Royal Heather':
                $retval[] = 'Royal';
                $retval[] = 'Tri-Blend Vintage Royal';
                break;
            case 'Vintage Royal':
                $retval[] = 'Tri-Blend Vintage Royal';
                break;
            case 'Dark Heather':
                $retval[] = 'Dark Grey Heather';
            case 'Premium Heather':
                $retval[] = 'Tri-Blend Premium Heather';
                break;
            case 'Navy':
                $retval[] = 'Solid Midnight Navy';
                break;
            case 'Vintage Navy':
                $retval[] = 'Tri-Blend Vintage Navy';
                break;
            case 'Black':
                $retval[] = 'Solid Black';
                break;
            case 'WHITE':
                $retval[] = 'Solid White';
                break;
            case 'Baby Blue':
                $retval[] = 'Light Blue';
                break;
            case 'Heather Grey':
            case 'Gray':
            case 'Lightgray':
            case 'Grey':
                $retval[] = 'Sport Grey';
                $retval[] = 'Heather';
                break;
            case 'Vintage Red':
                $retval[] = 'Tri-Blend Vintage Red';
                break;
            case 'Vintage Green':
                $retval[] = 'Irish Green';
                $retval[] = 'Tri-Blend Envy';
                break;
            case 'Vintage Black':
                $retval[] = 'Tri-Blend Vintage Black';
                break;
            case 'Detroit Lions - Charcoal':
            case 'Charcoal Heather':
            case 'Coal':
            case 'Heavygray':
            case 'Asphalt':
                $retval[] = 'Charcoal';
                break;
            case 'Sport grey':
                $retval[] = 'Heather Grey';
                break;
            case 'Black T-shirt':
            case 'Black Unisex T-shirt':
            case 'Tee - Black':
                $retval[] = 'Black';
                break;
            case 'Navy Blue':
            case 'Blue Navy':
            case 'Navy Heather':
                $retval[] = 'Navy';
                break;
            case 'Athletic Heather':
                $retval[] = 'Tri-Blend Premium Heather';
                break;
            case 'Heather White':
            case 'Vintage White':
                $retval[] = 'Tri-Blend Heather White';
                $retval[] = 'White';
                break;
            case 'Armygreen':
                $retval[] = 'Military Green';
                break;
            case 'Light Pink':
                $retval[] = 'Solid Light Pink';
                break;
            case 'Heather':
                $retval[] = 'Sport Grey';
                $retval[] = 'Tri-Blend Premium Heather';
                break;
            case 'Red Heather':
                $retval[] = 'Tri-Blend Vintage Red';
                break;
            case 'Navy Heather':
                $retval[] = 'Tri-Blend Vintage Navy';
                break;
            default:
                break;
        }
        return $retval;
    }

    public function sendRequest ($url, $token = null) {
        $token = $token ? $token : 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIzN2Q0YmQzMDM1ZmUxMWU5YTgwM2FiN2VlYjNjY2M5NyIsImp0aSI6ImZkM2M2YjZhZGQwM2ExYTk5NjdiODA2NzhlZTNhZmM4YmUyOGEzYmEzOWVmNDBlN2QzMDM0ZGExMzdhOTJiOTE3NWZhMTBiZWNlOTJlZDFiIiwiaWF0IjoxNjQyNDAwNjUwLCJuYmYiOjE2NDI0MDA2NTAsImV4cCI6MTY3MzkzNjY1MCwic3ViIjoiNzY4NjY2NSIsInNjb3BlcyI6WyJzaG9wcy5tYW5hZ2UiLCJzaG9wcy5yZWFkIiwiY2F0YWxvZy5yZWFkIiwib3JkZXJzLnJlYWQiLCJvcmRlcnMud3JpdGUiLCJwcm9kdWN0cy5yZWFkIiwicHJvZHVjdHMud3JpdGUiLCJ3ZWJob29rcy5yZWFkIiwid2ViaG9va3Mud3JpdGUiLCJ1cGxvYWRzLnJlYWQiLCJ1cGxvYWRzLndyaXRlIiwicHJpbnRfcHJvdmlkZXJzLnJlYWQiXX0.AELtGNbKChRzfQGNkLhEu2NI9krFUPU4gvuK4i-GxdLLEAwr5r_wSBg1t1BK6jHOYfDUfRcUK3cO0EeWhw0';
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
          CURLOPT_HTTPHEADER => array(
            'Authorization: Bearer ' .  $token
          ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return json_decode($response, 1);
    }
}
