package com.danding.yang800.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Entity.GoodsSortInfo;
import com.danding.yang800.Entity.RefundInfo;
import com.danding.yang800.General.SingleRequestQueue;
import com.danding.yang800.R;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.listener.OnConvertViewClickListener;

public class RefundAdapter extends MyBaseAdapter<RefundInfo> {
    private RequestQueue mQueue;


    public RefundAdapter(Context context) {
        super(context);
        mQueue = SingleRequestQueue.getRequestQueue(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater
                    .inflate(R.layout.refund_adapterlayout, null);
            info = new Info();
            info.refund_addLayout = (LinearLayout) convertView
                    .findViewById(R.id.refund_addLayout);
            info.refund_number = (TextView) convertView
                    .findViewById(R.id.refund_number);
            info.refund_reduceLayout = (LinearLayout) convertView
                    .findViewById(R.id.refund_reduceLayout);
            info.refund_adaptercheck = (CheckBox) convertView
                    .findViewById(R.id.refund_adaptercheck);
            info.refund_adapterIV = (ImageView) convertView
                    .findViewById(R.id.refund_adapterIV);
            info.refund_adaptername = (TextView) convertView
                    .findViewById(R.id.refund_adaptername);
            info.refund_adapternumber = (TextView) convertView
                    .findViewById(R.id.refund_adapternumber);
            info.refund_adapterpriceedit = (TextView) convertView
                    .findViewById(R.id.refund_adapterpriceedit);
            info.refund_adaptercheckLayout = (RelativeLayout) convertView
                    .findViewById(R.id.refund_adaptercheckLayout);
            final int num = Integer.parseInt(getItem(position).getInfo().getNumber());
            info.refund_addLayout.setOnClickListener(new OnConvertViewClickListener(convertView, R.id.ab__id_adapter_item_position) {
                @Override
                public void onClickCallBack(View registedView, int positionIds) {
                    int posi = Integer.parseInt(String.valueOf(registedView.getTag(positionIds)));
                    int number = Integer.parseInt(getItem(posi).getInfo().getNumber());
                    if (number >= num) {
                        ToastUtil.show(context, "退货数量不能大于购买数量");
                        return;
                    }
                    number++;
                    TextView refund_number = (TextView) registedView
                            .findViewById(R.id.refund_number);
                    TextView refund_adapternumber = (TextView) registedView
                            .findViewById(R.id.refund_adapternumber);
                    refund_adapternumber.setText("x" + number);
                    refund_number.setText(number + "");
                    getItem(posi).getInfo().setNumber(number + "");
                }
            });
            info.refund_adaptercheckLayout.setOnClickListener(new OnConvertViewClickListener(convertView, R.id.ab__id_adapter_item_position) {
                @Override
                public void onClickCallBack(View registedView, int positionIds) {
                    CheckBox refund_adaptercheck = (CheckBox) registedView
                            .findViewById(R.id.refund_adaptercheck);
                    refund_adaptercheck.setChecked(!getItem(position).isChecked());
                }
            });
            info.refund_reduceLayout.setOnClickListener(new OnConvertViewClickListener(convertView, R.id.ab__id_adapter_item_position) {
                @Override
                public void onClickCallBack(View registedView, int positionIds) {
                    int posi = Integer.parseInt(String.valueOf(registedView.getTag(positionIds)));
                    int number = Integer.parseInt(getItem(posi).getInfo().getNumber());

                    TextView refund_number = (TextView) registedView
                            .findViewById(R.id.refund_number);
                    if (number == 1) {
                        ToastUtil.show(context, "数量不能再少了");
                    } else {
                        number--;
                        TextView refund_adapternumber = (TextView) registedView
                                .findViewById(R.id.refund_adapternumber);
                        refund_adapternumber.setText("x" + number);
                        refund_number.setText(number + "");
                        getItem(posi).getInfo().setNumber(number + "");
                    }
                }
            });
//
//
            convertView.setTag(info);

        } else {
            info = (Info) convertView.getTag();
        }
        convertView.setTag(R.id.ab__id_adapter_item_position, position);
        info.refund_adaptercheck
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        // TODO Auto-generated method stub
                        getItem(position).setChecked(isChecked);
                    }
                });
        convertView.setTag(R.id.ab__id_adapter_item_position, position);
        Glide.with(context).load(getItem(position).getInfo().getUrl()).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(info.refund_adapterIV);
        info.refund_adaptername.setText(getItem(position).getInfo().getName());
        info.refund_adapternumber.setText("x" + getItem(position).getInfo().getNumber());
        info.refund_number.setText(getItem(position).getInfo().getNumber());
        info.refund_adapterpriceedit.setText("￥" + getItem(position).getInfo().getPrice());
        info.refund_adaptercheck.setChecked(getItem(position).isChecked());
        return convertView;
    }

    class Info {
        CheckBox refund_adaptercheck;
        ImageView refund_adapterIV;
        TextView refund_adaptername;
        TextView refund_adapternumber;
        TextView refund_adapterpriceedit;
        TextView refund_number;
        LinearLayout refund_reduceLayout, refund_addLayout;
        RelativeLayout refund_adaptercheckLayout;
    }
}
