<?php
/**
 * Created by PhpStorm.
 * User: ali
 * Date: 8/29/2020
 * Time: 8:38 PM
 */

namespace Mamad\Group_Product\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mamad\Brand\Repositories\BrandRepository;
use Mamad\Category\Repositories\CategoryRepository;
use Mamad\Comment\Repository\CommentRepository;
use Mamad\Group_Product\Http\Requests\AddGroupProductRequest;
use Mamad\Group_Product\Http\Requests\EditGroupProductRequest;
use Mamad\Group_Product\Repository\GroupChildrenProductRepository;
use Mamad\Group_Product\Repository\GroupProductRepository;
use Mamad\PostMeta\Repository\PostMetaRepository;
use Mamad\Product\Http\Requests\AddProductRequest;
use Mamad\Product\Http\Requests\EditProductRequest;
use Mamad\Product\Models\Product;
use Mamad\Product\Repository\ProductRepository;
use Mamad\Product\Repository\ProductVariationRepository;
use Mamad\ProductAttr\Models\Attribute;
use Mamad\ProductAttr\Repository\ProductAttrRepository;

class GroupProductController extends Controller
{
    // theme functions
    public function index($slug){
        $product=GroupProductRepository::find_by_slug($slug);

        $in_stock_children_products=[];
        $in_stock_variations=[];
        $filter_attributes=[];
            foreach ($product->children_products as $childrenId) {
                $childrenProduct=GroupChildrenProductRepository::find($childrenId);
                if(ProductRepository::in_stock($childrenProduct,1)){
                    $attributes=ProductRepository::get_use_for_product_attribute($childrenProduct);
                    $childrenProduct["filters_attribute"]=$attributes;
                    $filter_attributes[]=$attributes;
                    $in_stock_children_products[]=$childrenProduct;

                    if($childrenProduct->type == $childrenProduct->type_variable){
                        $variations=GroupProductRepository::get_product_variation($childrenProduct);
                        foreach ($variations as $variation) {
                            if(ProductRepository::in_stock($product,1,$variation["id"])){
                                $in_stock_variations[$childrenProduct->id][]=$variation;
                            }
                        }
                    }
                }
            }

            $filter_attributes_once=[];
            foreach ($filter_attributes as $filter_attribute) {
                foreach ($filter_attribute as $filter_attribute_item) {
                    if(!isset($filter_attributes_once[$filter_attribute_item["parent"]["id"]])){
                        $filter_attributes_once[$filter_attribute_item["parent"]["id"]]["parent"]=$filter_attribute_item["parent"];
                        $filter_attributes_once[$filter_attribute_item["parent"]["id"]]["values"][]=$filter_attribute_item["values"][0];
                    } else{
                        $new_filter_value=false;
                        foreach ($filter_attributes_once[$filter_attribute_item["parent"]["id"]]["values"] as $value) {
                            if($value["id"] !=$filter_attribute_item["values"][0]["id"]){
                                $new_filter_value=true;
                            }
                        }
                        if($new_filter_value){
                            $filter_attributes_once[$filter_attribute_item["parent"]["id"]]["values"][]=$filter_attribute_item["values"][0];
                        }
                    }
                }
            }
        return view("Front::group_product",compact("product","in_stock_children_products","in_stock_variations","filter_attributes_once"));
    }

    //admin functions
    public function product_list(Request $request)
    {
         $this->authorize("index",Product::class);
        $products=GroupProductRepository::order_product($request->orderBy,$request->name);

        return view("GroupProduct::Admin.product_list",["products"=>$products]);
    }

    public function product_add_form ()
    {
        $this->authorize("create",Product::class);
        $categories=CategoryRepository::get_by_type("group_product");
        $brands=BrandRepository::get_by_type("group_product");
        $child_products=GroupChildrenProductRepository::order_product("desc");
        return view("GroupProduct::Admin.product_add",["brands"=>$brands,"categories"=>$categories,"child_products"=>$child_products]);
    }

    public function product_add(AddGroupProductRequest $request)
    {
        $this->authorize("create",Product::class);
        $slug=make_slug_for_data($request->title,$request->slug);
        $request->request->add(["slug"=>$slug]);

        $product=GroupProductRepository::create($request);
        PostMetaRepository::update_meta_tag($product,$request);
        if($request->product_cat){
            GroupProductRepository::create_product_category($product,$request->product_cat);
        }
        if($request->product_brand){
            GroupProductRepository::create_product_brand($product,$request->product_brand);
        }

        return redirect()->route("group_product.add");
    }

    public function product_edit_form($id)
    {
        $this->authorize("edit",Product::class);
        $categories=CategoryRepository::get_by_type("group_product");
        $product=GroupProductRepository::find($id);
        $child_products=GroupChildrenProductRepository::order_product("desc");
        $brands=BrandRepository::get_by_type("group_product");
        $productBrands=ProductRepository::get_product_brands($product);
        $productCat=ProductRepository::get_product_cats($product);
        return view("GroupProduct::Admin.product_edit",["brands"=>$brands,"productBrands"=>$productBrands,"categories"=>$categories,"product"=>$product,"productCatId"=>$productCat,"child_products"=>$child_products]);
    }

    public function product_edit(EditGroupProductRequest $request)
    {
        $this->authorize("update",Product::class);
        $slug=make_slug_for_data($request->title,$request->slug);

        $request->request->add(["slug"=>$slug]);

        $product=GroupProductRepository::find($request->id);
        PostMetaRepository::update_meta_tag($product,$request);
        GroupProductRepository::update($product,$request);


        if($product->category){
            ProductRepository::delete_product_cats($product);
        }
        if($product->brand){
            ProductRepository::delete_product_brands($product);
        }
        if($request->product_brand){
            GroupProductRepository::create_product_brand($product,$request->product_brand);
        }

        if($request->product_cat){
            GroupProductRepository::create_product_category($product,$request->product_cat);
        }

        return back();
    }

    public function product_delete($id)
    {
        $this->authorize("delete",Product::class);
        $product=GroupProductRepository::find($id);

        if($product->category){
            ProductRepository::delete_product_cats($product);
        }
        if($product->brand){
            ProductRepository::delete_product_brands($product);
        }
        GroupProductRepository::destroy($id);
        return back();
    }

    public function product_get_attr(Request $request)
    {
        $this->authorize("create",Product::class);
        $this->authorize("edit",Product::class);

        $attr=ProductAttrRepository::find($request->attribute_id);
        $attr_value=$attr->sub_attr()->pluck("id","name");
        return $attr_value;
    }

    public function get_product_attribute_use_for_variable(Request $request,$product=null)
    {
        $this->authorize("create",Product::class);
        $this->authorize("edit",Product::class);
        if($product){
            $product=$product;
        } else{
            $product=$request->product_id;
        }
        $attributes=ProductRepository::get_use_for_variable_attribute($product);

        return $attributes;
    }

    public function save_product_attribute(Request $request)
    {
        $this->authorize("create",Product::class);
        $this->authorize("edit",Product::class);
        $product=ProductRepository::find($request->product_id);
       if($product){
           $result=ProductRepository::add_attr_to_product($product,$request);
       } else{
           $result=ProductRepository::add_attr_to_product((object) ["id"=>$request->product_id],$request);
        }
       return $result;
    }

    public  function attribute_delete(Request $request,$id=null){
        if(!is_null($id)){
        ProductAttrRepository::delete_product_attr($request->attribute_id,$id);
        }
        return true;
    }

    public function variation_delete(Request $request)
    {
        ProductVariationRepository::delete_product_one_variation($request->variation_id);

    }
}
