package com.yono.messeripos.models;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.annotations.SerializedName;
import com.yono.messeripos.R;
import com.yono.messeripos.api.ApiHelper;
import com.yono.messeripos.utils.Utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ProductModels<T> implements Parcelable {

    @SerializedName("id")
    private int idProduct;

    @SerializedName("name")
    private String productName;

    @SerializedName("price")
    private int priceProduct;

    @SerializedName("stock")
    private int stockProduct;

    @SerializedName("image")
    private String imageProduct;

    @SerializedName("category_id")
    private int categoryProduct;


    protected ProductModels(Parcel in) {
        idProduct = in.readInt();
        productName = in.readString();
        priceProduct = in.readInt();
        stockProduct = in.readInt();
        imageProduct = in.readString();
        categoryProduct = in.readInt();
    }

    public static final Creator<ProductModels> CREATOR = new Creator<ProductModels>() {
        @Override
        public ProductModels createFromParcel(Parcel in) {
            return new ProductModels(in);
        }

        @Override
        public ProductModels[] newArray(int size) {
            return new ProductModels[size];
        }
    };

    public int getIdProduct() {
        return idProduct;
    }

    public void setIdProduct(int idProduct) {
        this.idProduct = idProduct;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getPriceProduct() {
        return priceProduct;
    }

    public void setPriceProduct(int priceProduct) {
        this.priceProduct = priceProduct;
    }

    public int getStockProduct() {
        return stockProduct;
    }

    public void setStockProduct(int stockProduct) {
        this.stockProduct = stockProduct;
    }

    public int getCategoryProduct() {
        return categoryProduct;
    }

    public void setCategoryProduct(int categoryProduct) {
        this.categoryProduct = categoryProduct;
    }

    public String getImageProduct() {
        return imageProduct;
    }

    public void setImageProduct(String imageProduct) {
        this.imageProduct = imageProduct;
    }

    public ProductModels() {
    }

    public ProductModels(String productName, int priceProduct, int stockProduct, String imageProduct) {
        this.productName = productName;
        this.priceProduct = priceProduct;
        this.stockProduct = stockProduct;
        this.imageProduct = imageProduct;
    }

    @BindingAdapter("url")
    public static void setImage(ImageView view, String url){
        Utils utils = new Utils();
        if (TextUtils.isEmpty(url) || url.length() < 1000){
            view.setImageResource(R.drawable.ic_meser_create);
        }else{
//            Glide.with(view)
//                    .applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.skeleton).error(R.drawable.skeleton))
//                    .load(ApiHelper.BASE_URL_IMAGE+url)
//                    .into(view);
            view.setImageBitmap(utils.decodeImageBase64(url));
        }
    }

    @BindingAdapter("path")
    public static void setPathImage(ImageView view, String path) {
        Utils utils = new Utils();

        if (TextUtils.isEmpty(path)) view.setImageResource(R.drawable.ic_meser_create);
        else {
            view.setImageBitmap(utils.decodeImageBase64(path));
        }
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(idProduct);
        parcel.writeString(productName);
        parcel.writeInt(priceProduct);
        parcel.writeInt(stockProduct);
        parcel.writeString(imageProduct);
        parcel.writeInt(categoryProduct);
    }
}
