package com.rv.share.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ruiwenliu.wrapper.dialog.BaseDialog;
import com.ruiwenliu.wrapper.weight.ToastView;
import com.rv.share.R;
import com.rv.share.adapter.AllGoodsListAdapter;
import com.rv.share.bean.AllGoodsListBean;

import java.util.LinkedList;

public class AllPreCardDialog extends BaseDialog {

    private RecyclerView recyclerview;
    private TextView tv_add, tv_number;
    private LinearLayout ll_subtract;
    private Button btn_pay;
    private ImageView img_close;
    private int count = 1;

    private AllGoodsListAdapter listAdapter;
    private LinkedList<AllGoodsListBean.DetailList> datas = new LinkedList<>();

    private PayListener listener;

    public AllPreCardDialog(@NonNull Context context, AllGoodsListBean bean, PayListener listener) {
        super(context);
        setDialogParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        this.listener = listener;
        initData(bean);
        initView();
    }

    private void initData(AllGoodsListBean bean) {
        datas.clear();
        if (bean != null && bean.getData() != null && bean.getData().size() > 0) {
            for (AllGoodsListBean.GoodsItem item : bean.getData()) {
                String name = item.name;
                if (item.list != null && item.list.size() > 0) {
                    for (AllGoodsListBean.DetailList detailList : item.list) {
                        detailList.name = name;
                        datas.add(detailList);
                    }
                }
            }
        }
    }

    private void initView() {
        recyclerview = findViewById(R.id.recyclerview);
        ll_subtract = findViewById(R.id.ll_subtract);
        tv_add = findViewById(R.id.tv_add);
        tv_number = findViewById(R.id.tv_number);
        btn_pay = findViewById(R.id.btn_pay);
        img_close = findViewById(R.id.img_close);
        listAdapter = new AllGoodsListAdapter(datas);
        recyclerview.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setAdapter(listAdapter);

        ll_subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count == 1) {
                    Toast.makeText(getContext(),"数量已为1",Toast.LENGTH_SHORT).show();
//                    ToastView.getInstance(getContext()).showToastContent("数量已为1");
                    return;
                }
                count -= 1;
                tv_number.setText(String.valueOf(count));
            }
        });

        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int p) {
                AllGoodsListBean.DetailList item = (AllGoodsListBean.DetailList) adapter.getItem(p);
                if (item.stock <= 0) {
                    Toast.makeText(getContext(),"该预售卡已售罄！",Toast.LENGTH_SHORT).show();
//                    ToastView.getInstance(getContext()).showToastContent("该预售卡已售罄！");
                    return;
                }
                listAdapter.setSelected(p);
            }
        });

        tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AllGoodsListBean.DetailList item = listAdapter.getItem(listAdapter.getSelected());
                if (item.stock <= count) {
//                    ToastView.getInstance(getContext()).showToastContent("库存不足！");

                    Toast.makeText(getContext(),"库存不足",Toast.LENGTH_SHORT).show();
                    return;
                }
                count += 1;
                tv_number.setText(String.valueOf(count));
            }
        });

        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.pay(listAdapter.getItem(listAdapter.getSelected()), count);
                dismiss();
            }
        });

        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        setCancelable(false);
    }

    @Override
    public int getViewLayout() {
        return R.layout.dialog_all_pre_card;
    }

    public interface PayListener {
        void pay(AllGoodsListBean.DetailList detailList, int number);
    }
}
