package cn.gcs.base.gcsapp.activity.main.fg_shop.ac_shoppingCart.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.lang.reflect.Field;
import java.util.ArrayList;

import cn.gcs.base.gcsapp.R;
import cn.gcs.base.gcsapp.activity.main.fg_shop.ac_goodsDetail.m.CartGoods;
import cn.gcs.base.gcsapp.activity.main.fg_shop.ac_shoppingCart.ShoppingCartActivity;
import cn.gcs.base.gcsapp.utils.PixelUtil;


/**
 * Created by  on 2016/9/2.
 */
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.MyViewHolder> {
    private ArrayList<CartGoods> mData;
    private Context context;
    LayoutInflater layoutInflater;
    public ArrayList<Boolean> checks = new ArrayList<>();
    CheckChange checkChange;

    public boolean canChange = true;

    public ShoppingCartAdapter(Context mContext, ArrayList<CartGoods> data) {
        context = mContext;
        mData = data;
        layoutInflater = LayoutInflater.from(context);

        for (int i = 0; i < data.size(); i++) {
            checks.add(false);
            if (mData.get(i).kind == null) {
                mData.get(i).kind = "2";
            } else {
                mData.get(i).kind = mData.get(i).kind.equals("1") ? "1" : "2";
            }
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_rv_shopping_cart, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final CartGoods cartGoods = mData.get(position);

        holder.cb_item.setChecked(checks.get(position));
        holder.tv_item_name.setText(cartGoods.goodsName);
        holder.tv_item_type.setText(cartGoods.size + "/" + cartGoods.colour);
        holder.tv_item_num.setText(cartGoods.num + "");

        if (cartGoods.kind.equals("2")) {
            holder.iv_csb_jifen.setImageResource(R.mipmap.icon_shop_pricemammon);
            holder.iv_text_csb_jifen.setImageResource(R.mipmap.icon_text_caishenbi);
            holder.tv_item_price_one.setText(String.format("%.2f", cartGoods.priceMammon));
            holder.tv_item_price_one.setTextColor(context.getResources().getColor(R.color.colorAppRed));

            holder.tv_item_price_add.setText(String.format("%.2f", (cartGoods.num * cartGoods.priceMammon)));
            holder.tv_item_price_add.setTextColor(context.getResources().getColor(R.color.colorAppRed));
        } else {
            holder.iv_csb_jifen.setImageResource(R.mipmap.icon_shop_price_score);
            holder.iv_text_csb_jifen.setImageResource(R.mipmap.icon_text_jifen);
            holder.tv_item_price_one.setText(cartGoods.priceScore + "");
            holder.tv_item_price_one.setTextColor(context.getResources().getColor(R.color.color_yellow_e9));

            holder.tv_item_price_add.setText((cartGoods.num * cartGoods.priceScore) + "");
            holder.tv_item_price_add.setTextColor(context.getResources().getColor(R.color.color_yellow_e9));
        }
        if (cartGoods.detailPic != "") {
            Glide.with(context).load(cartGoods.detailPic).into(holder.iv_item_goods);
        }

        holder.iv_item_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (canChange) {
                    canChange = false;
                    cartGoods.num++;
                    holder.tv_item_num.setText(cartGoods.num + "");
                    if (cartGoods.kind.equals("2")) {
                        holder.tv_item_price_add.setText(String.format("%.2f", (cartGoods.num * cartGoods.priceMammon)));
                    } else {
                        holder.tv_item_price_add.setText((cartGoods.num * cartGoods.priceScore) + "");
                    }

                    checkChange.change();
                    ShoppingCartActivity.instance.changeCartsNum(cartGoods.cartId, cartGoods.num);
                }
            }
        });
        holder.iv_item_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cartGoods.num > 1) {
                    if (canChange) {
                        canChange = false;
                        cartGoods.num--;
                        holder.tv_item_num.setText(cartGoods.num + "");
                        if (cartGoods.kind.equals("2")) {
                            holder.tv_item_price_add.setText(String.format("%.2f", (cartGoods.num * cartGoods.priceMammon)));
                        } else {
                            holder.tv_item_price_add.setText((cartGoods.num * cartGoods.priceScore) + "");
                        }

                        checkChange.change();
                        ShoppingCartActivity.instance.changeCartsNum(cartGoods.cartId, cartGoods.num);
                    }
                }
            }
        });

        holder.rl_cb_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.cb_item.setChecked(holder.cb_item.isChecked() ? false : true);
                checks.set(position, holder.cb_item.isChecked());
                checkChange.change();
            }
        });

//        holder.dl_item_cart.setScrimColor(0x40000000);
//        setDrawerLeftEdgeSize(context, holder.dl_item_cart, 0.3f);

        holder.tv_item_cart_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShoppingCartActivity.instance.deleteFromCarts(cartGoods.cartId);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_cb_item;
        CheckBox cb_item;

        ImageView iv_item_goods;
        ImageView iv_item_plus;
        ImageView iv_item_reduce;
        ImageView iv_csb_jifen;
        ImageView iv_text_csb_jifen;

        TextView tv_item_name;
        TextView tv_item_price_one;
        TextView tv_item_type;
        TextView tv_item_price_add;
        TextView tv_item_num;

//        DrawerLayout dl_item_cart;
        TextView tv_item_cart_delete;

        public MyViewHolder(final View itemView) {
            super(itemView);
            rl_cb_item = (RelativeLayout) itemView.findViewById(R.id.rl_cb_item);
            cb_item = (CheckBox) itemView.findViewById(R.id.cb_item);

            iv_item_goods = (ImageView) itemView.findViewById(R.id.iv_item_goods);
            iv_item_plus = (ImageView) itemView.findViewById(R.id.iv_item_plus);
            iv_item_reduce = (ImageView) itemView.findViewById(R.id.iv_item_reduce);
            iv_csb_jifen = (ImageView) itemView.findViewById(R.id.iv_csb_jifen);
            iv_text_csb_jifen = (ImageView) itemView.findViewById(R.id.iv_text_csb_jifen);

            tv_item_name = (TextView) itemView.findViewById(R.id.tv_item_name);
            tv_item_price_one = (TextView) itemView.findViewById(R.id.tv_item_price_one);
            tv_item_type = (TextView) itemView.findViewById(R.id.tv_item_type);
            tv_item_price_add = (TextView) itemView.findViewById(R.id.tv_item_price_add);
            tv_item_num = (TextView) itemView.findViewById(R.id.tv_item_num);

//            dl_item_cart = (DrawerLayout) itemView.findViewById(R.id.dl_item_cart);
            tv_item_cart_delete = (TextView) itemView.findViewById(R.id.tv_item_cart_delete);
        }
    }

    public void setCheckAll(boolean checkAll){
        for(int i=0;i<mData.size();i++) {
            checks.set(i, checkAll);
            notifyDataSetChanged();
            checkChange.change();
        }
    }

    public interface CheckChange{
        void change();
    }

    public void setCheckChange(CheckChange mCheckChange){
        checkChange = mCheckChange;
    }

    private void setDrawerLeftEdgeSize (Context context, DrawerLayout drawerLayout, float displayWidthPercentage) {
        if (context == null || drawerLayout == null) return;
        try {
            // 找到 ViewDragHelper 并设置 Accessible 为true
            Field leftDraggerField =
                    drawerLayout.getClass().getDeclaredField("mRightDragger");//Right
            leftDraggerField.setAccessible(true);
            ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);

            // 找到 edgeSizeField 并设置 Accessible 为true
            Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
            edgeSizeField.setAccessible(true);
            int edgeSize = edgeSizeField.getInt(leftDragger);

            // 设置新的边缘大小
            int screenWidth = PixelUtil.getWidth(context);
            edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (screenWidth * displayWidthPercentage)));
        } catch (Exception e) {
            Log.e("aaa", "setDrawerLeftEdgeSize wrong");
        }
    }

}