<?php

namespace Hura8\Components\Product\AdminController;

use Hura8\Components\Product\Model\ProductVariantModel;

class AProductVariantController
{
    /* @var ProductVariantModel $objProductVariantModel */
    protected $objProductVariantModel;


    public function __construct($product_id)
    {
        $this->objProductVariantModel = new ProductVariantModel($product_id);
    }


    public function delete($id) {
        return $this->objProductVariantModel->delete($id);
    }


    public function updateImage($variant_id, $image_name) {
        return $this->objProductVariantModel->updateFields($variant_id, [
            "thumbnail" => $image_name
        ]);
    }

    public function removeImage($variant_id) {
        return $this->objProductVariantModel->updateFields($variant_id, [
            "thumbnail" => ''
        ]);
    }


    public function getInfo($id) {
        return $this->objProductVariantModel->getInfo($id);
    }

    public function getProductVariantOption($product_id){
        return $this->objProductVariantModel->getProductVariantOption($product_id);
    }


    //update product's variant options
    public function updateVariantOption($attribute) {
        return $this->objProductVariantModel->updateVariantOption($attribute);
    }

    public function useVariantOptionSample($select_id) {
        $sample = $this->objProductVariantModel->useVariantOptionSample($select_id);

        return ($sample) ? \json_decode($sample,true) : [];
    }


    //use a product's variant-option to create a choice, so next product can select without recreate from beginning
    public function createVariantOptionSample($use_from_pro_id, $sample_title) {
        return $this->objProductVariantModel->createVariantOptionSample($use_from_pro_id, $sample_title);
    }


    public function getVariantOptionSample() {
        return $this->objProductVariantModel->getVariantOptionSample();
    }

    public function updateVariant($variant_id, array $variant_info) {
        if($variant_id) {
            return $this->objProductVariantModel->update($variant_id, $variant_info);
        }else{
            return $this->objProductVariantModel->create($variant_info);
        }
    }

    public function getProductVariantPriceRange(){
        return $this->objProductVariantModel->getProductVariantPriceRange();
    }

    public function getProductVariantList(){
        return $this->objProductVariantModel->getList([]);
    }


}
