<?php

namespace App\Repositories;



use App\Models\CountryPrice;
use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductCategory;
use App\Models\ProductImage;
use App\Models\ProductIngredient;

class ProductRepository extends BaseRepository {


    public function __construct(Product $model)
    {
        $this->model = $model;
    }


    /**
     * refresh.
     */
    public function update($data)
    {

        $data['active'] = isset($data['active']) ? 1 : 0;
        $data['single_commission'] = isset($data['single_commission']) ? 1 : 0;
        $data['amount_commission'] = isset($data['amount_commission']) ? 1 : 0;
        if($data['id'] === "new"){
            $this->model = Product::create($data);
        }
        else{
            $this->model = $this->getById($data['id']);
            $this->model->fill($data);
            $this->model->save();
        }

        $this->updateCategories(isset($data['categories']) ? $data['categories'] : array());
        $this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array());
        $this->updateIngredients(isset($data['product_ingredients']) ? $data['product_ingredients'] : array());
        $this->updateCountryPrices($data);


        return $this->model;
    }


    public function updateIngredients($data = array())
    {
        $ProductIngredient = $this->model->p_ingredients()->pluck('ingredient_id')->toArray();
        //set attr
        if(is_array($data)){
            foreach ($data as $id) {
                //not use
                if(!in_array($id, $ProductIngredient)){
                    ProductIngredient::create([
                        'product_id' => $this->model->id,
                        'ingredient_id' => $id,
                    ]);
                }
            }
        }
        return true;
    }

    public function updateCategories($data = array())
    {
        foreach ($this->model->categories as $category) {
            if(($pos = array_search($category->category_id, $data)) !== FALSE){
                unset($data[$pos]);
            }else{
                $category->delete();
            }
        }
        //set attr
        if(is_array($data)){
            foreach ($data as $id) {
                ProductCategory::create([
                    'product_id' => $this->model->id,
                    'category_id' => $id,
                ]);
            }
        }
        return true;
    }

    public function updateAttributes($data = array())
    {
        foreach ($this->model->attributes as $attribute) {
            if(($pos = array_search($attribute->attribute_id, $data)) !== FALSE){
                unset($data[$pos]);
            }else{
                $attribute->delete();
            }
        }
        //set attr
        if(is_array($data)){
            foreach ($data as $id) {
                ProductAttribute::create([
                    'product_id' => $this->model->id,
                    'attribute_id' => $id,
                ]);
            }
        }
        return true;
    }

    public function updateCountryPrices($data)
    {
        if(!isset($data['country_prices']) || !is_array($data['country_prices'])){
            return false;
        }
        foreach ($data['country_prices'] as $k => $country_id) {
            $cp = CountryPrice::updateOrCreate([
                'country_id' => $country_id,
                'product_id' => $this->model->id,
            ],
            [
                'c_price' => isset($data['c_price'][$country_id]) ? reFormatNumber($data['c_price'][$country_id]) : null,
                'c_tax' => isset($data['c_tax'][$country_id]) ? reFormatNumber($data['c_tax'][$country_id]) : null,
                'c_price_old' => isset($data['c_price_old'][$country_id]) ? reFormatNumber($data['c_price_old'][$country_id]) : null,
                'c_currency' => isset($data['c_currency'][$country_id]) ? reFormatNumber($data['c_currency'][$country_id]) : null,
            ]);

        }



        return true;
    }


    public function copy($model)
    {
        $this->model = $model->replicate();
        $this->model->name = "Kopie: ".$this->model->name;
        $this->model->wp_number = null;
        $this->model->save();

        //categories
        foreach ($model->categories as $category){
            ProductCategory::create([
                'product_id' => $this->model->id,
                'category_id' => $category->category_id,
            ]);
        }

        //attributes
        foreach ($model->attributes as $attribute){
            ProductAttribute::create([
                'product_id' => $this->model->id,
                'attribute_id' => $attribute->attribute_id,
            ]);
        }

        //images
        foreach ($model->images as $image){
            $name = \App\Services\Slim::sanitizeFileName($image->original_name);
            $name = uniqid() . '_' . $name;

            //copy
            $data = \Storage::disk('public')->copy(
                'images/product/'.$image->product_id.'/'.$image->filename,
                'images/product/'.$this->model->id.'/'.$name
            );


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


        return $this->model;
    }






    public function delete()
    {

    }


}