package com.ihaoin.hooloo.device.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.config.Base;
import com.ihaoin.hooloo.device.data.ScrollBean;
import com.ihaoin.hooloo.device.data.vo.Goods;
import com.ihaoin.hooloo.device.data.vo.Sku;
import com.ihaoin.hooloo.device.util.CollectionUtils;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;

import java.util.Arrays;

public class GoodsAdapter extends BaseSectionQuickAdapter<ScrollBean, BaseViewHolder> {
    private Context mContext;

    public GoodsAdapter(Context context, int layoutResId, int sectionHeadResId) {
        super(layoutResId, sectionHeadResId, null);
        this.mContext = context;
    }

    @Override
    protected void convertHead(BaseViewHolder helper, ScrollBean item) {
        helper.setText(R.id.txt_category, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, ScrollBean item) {
        ScrollBean.ScrollItemBean t = item.t;
        Goods goods = (Goods) t.getObject();
        helper.setText(R.id.txt_name, goods.getName());
        if (StringUtils.isEmpty(goods.getDesc())) {
            helper.setGone(R.id.txt_intro, false);
        } else {
            helper.setGone(R.id.txt_intro, true);
            helper.setText(R.id.txt_intro, goods.getDesc());
        }

        if (CollectionUtils.isEmpty(goods.getTags())) {
            helper.setGone(R.id.layout_tags, false);
        } else {
            helper.setGone(R.id.layout_tags, true);
            LinearLayout layoutTag = helper.getView(R.id.layout_tags);
            layoutTag.removeAllViews();
            goods.getTags().forEach(x -> {
                View tagView = LayoutInflater.from(mContext).inflate(R.layout.item_tag, null);
                ((TextView) tagView.findViewById(R.id.txt_tag)).setText(x);
                layoutTag.addView(tagView);
            });
        }

        ImageView imgThumbnail = helper.getView(R.id.img_thumbnail);
        RoundedCorners roundedCorners = new RoundedCorners(5);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        if (goods.getPics() == null) {
            imgThumbnail.setImageDrawable(null);
        } else {
            Glide.with(mContext).load(goods.getPics().getThumbnail()).apply(options).into(imgThumbnail);
        }

        Sku sku = Utils.getDefaultSku(goods.getSkus());
        if (sku == null) {
            helper.setText(R.id.txt_price, Utils.toString(goods.getPrice()));
            helper.setText(R.id.txt_discount, Utils.toString(goods.getDiscount()));

            helper.setGone(R.id.txt_sellout, true);
            helper.setGone(R.id.butn_add, false);
        } else {
            helper.setText(R.id.txt_price, Utils.toString(sku.getPrice()));
            helper.setText(R.id.txt_discount, Utils.toString(sku.getDiscount()));

            if (Base.TRUE.equals(sku.getState())) {
                helper.setGone(R.id.txt_sellout, false);
                helper.setGone(R.id.butn_add, true);
                View butnAdd = helper.getView(R.id.butn_add);
                butnAdd.setOnClickListener(v -> Utils.sendAddGoodsBroadcast(mContext, goods, sku, 1));

                ViewGroup layoutItem = helper.getView(R.id.layout_item);
                Utils.setTouchDelegate(layoutItem, Arrays.asList(butnAdd));
            } else {
                helper.setGone(R.id.txt_sellout, true);
                helper.setGone(R.id.butn_add, false);
            }
        }

        if (Base.TRUE.equals(goods.getIsRecommend())) {
            helper.setVisible(R.id.img_recommend, true);
        } else {
            helper.setGone(R.id.img_recommend, false);
        }

        TextView icoPrice = helper.getView(R.id.ico_price);
        TextView txtPrice = helper.getView(R.id.txt_price);
        icoPrice.setPaintFlags(icoPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        txtPrice.setPaintFlags(txtPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

        View layoutItem = helper.getView(R.id.layout_item);
        layoutItem.setOnClickListener(v -> Utils.showGoodsDetail(mContext, goods));

        int padding = Utils.getDimens(mContext, R.dimen.padding10);
        layoutItem.setPadding(padding, padding, padding, padding);

        // 最后一个商品 并且购物条已经显示
        if (getData().indexOf(item) == getItemCount() - 1 && trolleyState) {
            int bottomPadding = Utils.getDimens(mContext, R.dimen.trolley_bar_height);
            layoutItem.setPadding(padding, padding, padding, bottomPadding);
        }
    }

    private boolean trolleyState = false;

    public boolean isTrolleyState() {
        return trolleyState;
    }

    public void setTrolleyState(boolean trolleyState) {
        this.trolleyState = trolleyState;
    }
}
