<?php

namespace App\Livewire;

use Exception;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Livewire\Component;

class OrderProductsCreate extends Component
{
    /** @var Collection<string, int> */
    public Collection $productInputs;

    /** @var Collection<string, int> */
    public Collection $serviceInputs;

    /** @var array<int> */
    public array $sizes = [];

    /** @var array<int> */
    public array $totals = [];

    /** @var array<int> */
    public array $units = [];

    /**
     * @var array<int>
     */
    public array $prices = [];

    /**
     * @var array<int>
     */
    public array $priceTotals = [];

    public int $totalQuantity = 0;

    public string $totalPrice = '$0.00';

    public function updated(): void
    {
        try {
            foreach ($this->sizes as $index => $size) {
                $this->totals[$index] = array_sum($size);
            }
        } catch (Exception $e) {
        }

        try {
            foreach ($this->units as $index => $unit) {
                $this->priceTotals[$index] = $unit * $this->prices[$index];
            }
        } catch (Exception $e) {
        }

        $this->totalQuantity = array_sum($this->totals);

        $this->totalPrice = '$'.number_format(round(array_sum($this->priceTotals), 2), 2);

    }

    public function addProductInput(): void
    {
        $index = $this->productInputs->count();
        $this->productInputs->push([
            $index => [
                'sku'           => '',
                'product_name'  => '',
                'product_color' => '',
                'size_xs'       => '',
                'size_s'        => '',
                'size_m'        => '',
                'size_l'        => '',
                'size_xl'       => '',
                'size_2xl'      => '',
                'size_3xl'      => '',
                'size_osfa'     => '',
                'product_total' => '',
            ],
        ]);
    }

    public function determineAddProductRow(int $index): void
    {
        if ($index == $this->productInputs->count() - 1) {
            $this->addProductInput();
        }
    }

    public function determineAddServiceProductRow(int $index): void
    {
        if ($index == $this->serviceInputs->count() - 1) {
            $this->addServiceInput();
        }
    }

    public function removeProductInput(int $key): void
    {
        if ($this->productInputs->count() > 1) {
            $this->productInputs->pull($key);
        }
    }

    public function addServiceInput(): void
    {
        $this->serviceInputs->push([
            $this->serviceInputs->count() => [
                'service_name'        => '',
                'product_name'        => '',
                'product_color'       => '',
                'logo_name'           => '',
                'setup_number'        => '',
                'service_width'       => '',
                'service_height'      => '',
                'service_setup_unit'  => '',
                'service_setup_price' => '',
                'service_total'       => '',
            ],
        ]);
    }

    public function removeServiceInput(int $key): void
    {
        if ($this->serviceInputs->count() > 1) {
            $this->serviceInputs->pull($key);
        }
    }

    public function mount(): void
    {
        $this->fill([
            'productInputs' => collect([
                [
                    'sku'           => '',
                    'product_name'  => '',
                    'product_color' => '',
                    'size_xs'       => '',
                    'size_s'        => '',
                    'size_m'        => '',
                    'size_l'        => '',
                    'size_xl'       => '',
                    'size_2xl'      => '',
                    'size_3xl'      => '',
                    'size_osfa'     => '',
                    'product_total' => '0',
                ],
            ]),
            'serviceInputs' => collect([
                [
                    'sku'                 => '',
                    'product_name'        => '',
                    'product_color'       => '',
                    'logo_name'           => '',
                    'setup_number'        => '',
                    'service_width'       => '',
                    'service_height'      => '',
                    'service_setup_unit'  => '',
                    'service_setup_price' => '',
                    'service_total'       => '',
                ],
            ]),
        ]);
    }

    public function render(): \Illuminate\Contracts\View\View|Factory|Application|View
    {
        return view('livewire.order-products-create');
    }
}
