package com.rv.share.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.rv.share.R;
import com.rv.share.bean.AllGoodsListBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Set;

public class AllGoodsListAdapter extends BaseQuickAdapter<AllGoodsListBean.DetailList, BaseViewHolder> {

    private int lastSelelcted;

    private int selected;

    public AllGoodsListAdapter(@Nullable List<AllGoodsListBean.DetailList> data) {
        super(R.layout.goods_item, data);

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).stock > 0) {
                lastSelelcted = selected = 0;
                break;
            }
        }
    }

    public int getSelected() {
        return selected;
    }

    public void setSelected(int position) {
        lastSelelcted = selected;
        selected = position;
        notifyItemChanged(lastSelelcted);
        notifyItemChanged(selected);
    }

    @Override
    protected void convert(BaseViewHolder helper, AllGoodsListBean.DetailList item) {


        if (getData().indexOf(item) == selected) {
            helper.setBackgroundRes(R.id.ll_content, R.drawable.shaper_bg_good_item_select);
            helper.setTextColor(R.id.tv_price, Color.parseColor("#FFB74B"));
            helper.setTextColor(R.id.tv_real_price, Color.parseColor("#FFB74B"));
        } else {
            helper.setBackgroundRes(R.id.ll_content, R.drawable.shaper_bg_good_item_unselect);
            helper.setTextColor(R.id.tv_price, Color.parseColor("#666666"));
            helper.setTextColor(R.id.tv_real_price, Color.parseColor("#666666"));
        }

        helper.setText(R.id.tv_real_price, "实付" + item.goodsPrice);
        try {
            JSONObject object = new JSONObject(item.goodsSpecs);
            helper.setText(R.id.tv_price, "¥ " + object.getDouble("price"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
