<?php

namespace App\Orchid\Screens\Product;

use Orchid\Screen\Screen;
// Screens
use Orchid\Screen\Fields\Input;
use Orchid\Screen\Fields\Select;
use Orchid\Support\Facades\Alert;
use Orchid\Screen\Actions\Button;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\Fields\TextArea;
use Orchid\Screen\Fields\CheckBox;
use Orchid\Screen\Fields\Group;

// Other
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Orchid\Screen\Fields\Relation;

// Models
use App\Models\Tag;
use App\Models\Color;
use App\Models\Product;
use App\Models\Category;
use App\Models\Brand;
use Orchid\Screen\Layouts\Rows;

class ProductCreateScreen extends Screen {

  public $colors;
  public $product;
  public $brands;
  public $categories;

  public function query(Product $product): iterable {
    return [
      'product' => $product,
      'colors' => Color::all(),
      'brands' => Brand::all(),
      'categories' => Category::all(),
    ];
  }

  public function name(): ?string {
    return 'Creating a new product';
  }

  public function commandBar(): iterable {
    return [
      Button::make('Create product')
        ->icon('pencil')
        ->method('create')
    ];
  }

  public function layout(): iterable {
    return [
      Layout::rows([
        Input::make('product.name')
          ->required()
          ->title(__('Name'))
          ->placeholder(__('Product name')),

        TextArea::make('product.description')
          ->rows(5)
          ->required()
          ->title(__('Description'))
          ->placeholder(__('Product description')),

        Input::make('product.price')
          ->title(__('Price'))
          ->required()
          ->mask([
            'alias' => 'currency',
            'groupSeparator' => '',
          ]),


        Input::make('product.old_price')
          ->title(__('Old price'))
          ->mask([
            'alias' => 'currency',
            'groupSeparator' => ''
          ]),

        Select::make('product.colors')
          ->multiple()
          ->title('Colors')
          ->disabled(!$this->colors->count())
          ->fromModel(
            Color::class,
            'name',
            'name',
          )
          ->placeholder($this->colors->count() ? 'Select colors' : 'Please create colors first in the "Colors" section.'),

        Select::make('product.brand')
          ->title('Brand')
          ->disabled(!$this->brands->count())
          ->fromModel(
            Brand::class,
            'name',
            'name',
          )
          ->placeholder($this->brands->count() ? 'Select brands' : 'Please create brands first in the "Brands" section.'),

        Select::make('product.categories')
          ->multiple()
          ->title('Categories')
          ->disabled(!$this->categories->count())
          ->fromModel(Category::class, 'name', 'name')
          ->placeholder($this->categories->count() ? 'Select categories' : 'Please create categories first in the "Categories" section.'),

        Input::make('product.image')
          ->type('file')
          ->title('Image')
          ->required(!$this->product->exists)
          ->help('Recommended size: 800x1000'),

        Input::make('product.images')
          ->type('file')
          ->title('Images')
          ->multiple()
          ->required(!$this->product->exists)
          ->help('Recommended size: 1500x2000'),

      ]),

      Layout::rows([
        CheckBox::make('product.is_bestseller')
          ->value(0)
          ->sendTrueOrFalse()
          ->placeholder(__('Bestseller')),

        CheckBox::make('product.is_featured')
          ->value(0)
          ->sendTrueOrFalse()
          ->placeholder(__('Featured')),

        CheckBox::make('product.is_new')
          ->value(0)
          ->sendTrueOrFalse()
          ->placeholder(__('New')),
      ])
    ];
  }

  public function create(Request $request) {

    $request->validate([
      'product.name' => 'required|unique:products,name',
      'product.description' => 'required',
      'product.price' => 'required',
      'product.image' => 'required|image|max:2048',
      'product.images' => 'required',
    ]);

    $image = $request->hasFile('product.image');
    $image && $path = $request->file('product.image')->store('', 'public');
    $image && $this->product->image = asset('storage/' . $path);

    $images = $request->file('product.images');
    $multipleImages = [];
    foreach ($images as $image) {
      if ($image) {
        $path = $image->store('', 'public');
        $multipleImages[] = asset('storage/' . $path);
        $this->product->images = json_encode($multipleImages);
      }
    }

    $this->product->name = ucfirst($request->input('product.name'));
    $this->product->description = ucfirst($request->input('product.description'));
    $this->product->price = $request->input('product.price');
    $this->product->old_price = $request->input('product.old_price');
    $this->product->colors = $request->input('product.colors');
    $this->product->categories = $request->input('product.categories');
    $this->product->brand = $request->input('product.brand');
    $this->product->is_bestseller = $request->input('product.is_bestseller');
    $this->product->is_featured = $request->input('product.is_featured');
    $this->product->is_new = $request->input('product.is_new');

    $this->product->save();
    Alert::info('You have successfully created the product.');
    return redirect()->route('platform.product.list');
  }
}
