<?php

namespace App\Http\Controllers;

use App\Models\Product;
use App\Models\ProductImage;
use App\Repositories\ProductRepository;
use Request;
use Validator;



class ImportProductController extends Controller
{
    protected $productRepo;

    public function __construct(ProductRepository $productRepo)
    {
        $this->middleware('admin');
        $this->productRepo = $productRepo;

    }

      public function import(){

        $path = app_path().'/../_static/products/';

        include($path.'_all_products.php');

        $slugs = array();

        foreach ($get_products as $c_id => $values){

            foreach ($values as $val){
                if(in_array($val['slug'], $slugs)){
                    continue;
                }
                $slugs[] = $val['slug'];

                    include($path.$val['slug'].'.php');

                    $data = [
                        'id' => 'new',
                        'name' => $val['name'],
                        'title' => '',
                        'copy' => $copy,
                        'price' => $price,
                        'price_ek' => 0,
                        'tax' => 19,
                        'price_old' => null,
                        'contents' => $content,
                        'number' => $item_no,
                        'icons' => $icons,
                        'description' => $description,
                        'usage' => $usage,
                        'ingredients' => $ingredients,
                        'pos' => null,
                        'amount' => 0,
                        'active' => 1,
                        'categories' => array($c_id),
                        ];


                $product = $this->productRepo->update($data);
                //images
                foreach($images as $image){
                    $i_path = storage_path().'/'.'app'.'/products/' .$val['slug'].'/'.$image['image'];
                    $mine = \File::mimeType($i_path);
                    $ext = \File::extension($i_path);
                    $size = \File::size($i_path);
                    $original_name = $image['image'];

                    $name = \App\Services\Slim::sanitizeFileName($image['image']);
                    $name = uniqid() . '_' . $name;

                    $img = \Image::make($i_path);
                    $img->resize(600, 800, function ($c) {
                     //   $c->aspectRatio();
                        $c->upsize();
                    });
                    //
                    \Storage::put('/public/images/product/'.$product->id.'/'.$name, (string) $img->encode());


                    ProductImage::create([
                        'product_id' => $product->id,
                        'filename' => $name,
                        'original_name' => $original_name,
                        'ext' => $ext,
                        'mine' => $mine,
                        'size' => $size
                    ]);
                }
            }

        }


        die("okay");
        //array('slug' => 'aloe-vera-gel99', 'name' => 'Aloe Vera Gel 99%', 'first' => 'aloe-vera-gel99-1.jpg', 'hover' => 'aloe-vera-gel99-2.jpg'),

    }



}