package com.bokecc.livemodule.view.gift;

import android.content.Context;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bokecc.gift.pojo.GiftInfo;
import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.chat.KeyboardHeightObserver;
import com.bokecc.livemodule.utils.DensityUtil;
import com.bokecc.livemodule.view.SelectGiftNumAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class GiftListView extends LinearLayout implements KeyboardHeightObserver{
    public static final String MAX_GIFT_COUNT = "99";
    public static final String price_des_header = "合计：￥";
    private EditText et_num;
    private TextView tv_gift_sum_price;
    private Button btn_send_gift;
    //    private String sum_price = price_des_header;
    private RecyclerView rv_gift_list;
    private ArrayList<GiftInfo> giftList = new ArrayList<GiftInfo>();
    private ListView lv_select_gift_num;
    private GiftListAdapter giftListAdapter;
    public static final String[] popwindowString = {"1", "2", "3", "4", "5"};
    private GiftInfo giftInfo;
//    private KeyboardHeightProvider keyboardHeightProvider;
    private View view;

    public GiftListView(Context context) {
        super(context);
        initView(context);
    }

    public GiftListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public GiftListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GiftListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        view = View.inflate(context, R.layout.view_gift_list, null);
        addView(view);

        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (lv_select_gift_num.getVisibility() == GONE) {
                    lv_select_gift_num.setVisibility(VISIBLE);
                }
            }
        });

        view.findViewById(R.id.close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setVisibility(GONE);
            }
        });

        et_num = view.findViewById(R.id.et_num);
        et_num.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                lv_select_gift_num.setVisibility(VISIBLE);
            }
        });
        et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
//                int giftNum = Integer.parseInt(editable.toString());
////                sum_price = price_des_header + giftNum *
                if (!TextUtils.isEmpty(editable.toString())) {
                    String giftCount = Integer.parseInt(editable.toString()) > 99 ? MAX_GIFT_COUNT : editable.toString();
                    et_num.removeTextChangedListener(this);
                    et_num.setText(giftCount);
                    Selection.setSelection(et_num.getText(), et_num.getText().toString().trim().length());
                    String sum_price = getGiftPrice();
                    tv_gift_sum_price.setText(price_des_header + sum_price);
                    et_num.addTextChangedListener(this);
                }
            }
        });

        tv_gift_sum_price = view.findViewById(R.id.tv_gift_sum_price);

        btn_send_gift = view.findViewById(R.id.btn_send_gift);
        btn_send_gift.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String sumGiftPerice = getGiftPrice();
                if (onClickGiftListListener != null && giftInfo != null && !TextUtils.isEmpty(et_num.getText().toString().trim())) {
                    onClickGiftListListener.onClickSendGift(sumGiftPerice, giftInfo, Integer.parseInt(et_num.getText().toString().trim()));
                }
            }
        });

        rv_gift_list = view.findViewById(R.id.rv_gift_list);
        int count = 3;
        if (DensityUtil.isTablet(context)){
            count = 5;
        }
        rv_gift_list.setLayoutManager(new GridLayoutManager(context, count));
        giftListAdapter = new GiftListAdapter(context, giftList, new GiftListAdapter.OnClickOnItemClickListener() {
            @Override
            public void onClickItem(GiftInfo giftInfo) {
                GiftListView.this.giftInfo = giftInfo;
                BigDecimal giftPrice = new BigDecimal(giftInfo.getGiftPrice());
                btn_send_gift.setTag(giftPrice);
                String sumPrice = getGiftPrice();
                tv_gift_sum_price.setText(price_des_header + sumPrice);
                if (lv_select_gift_num.getVisibility() == VISIBLE) {
                    lv_select_gift_num.setVisibility(GONE);
                }
            }
        });
        rv_gift_list.setAdapter(giftListAdapter);

        lv_select_gift_num = view.findViewById(R.id.lv_select_gift_num);
        SelectGiftNumAdapter selectGiftNumAdapter = new SelectGiftNumAdapter(getContext(), popwindowString);
        lv_select_gift_num.setAdapter(selectGiftNumAdapter);
        lv_select_gift_num.setVisibility(GONE);

        lv_select_gift_num.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                et_num.setText(popwindowString[i]);
                lv_select_gift_num.setVisibility(GONE);
            }
        });
    }

    public String getGiftPrice() {
        String sumPrice = "0.00";
        if (!TextUtils.isEmpty(et_num.getText().toString().trim())) {
            int giftNum = Integer.parseInt(et_num.getText().toString().trim());
            BigDecimal giftPrice = (BigDecimal) btn_send_gift.getTag();
            if (giftNum > 0 && giftPrice != null) {
                BigDecimal sumGiftPrice = giftPrice.multiply(new BigDecimal(giftNum));
                sumPrice = sumGiftPrice.toString();
            }
        }

        return sumPrice;
    }

    @Override
    public void onKeyboardHeightChanged(int height, int orientation) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.bottomMargin = height;
        view.setLayoutParams(params);
    }


    public interface OnClickGiftListListener {

        void onClickSendGift(String sumGiftPerice, GiftInfo giftInfo, int num);
    }

    private OnClickGiftListListener onClickGiftListListener;

    public void setOnClickGiftListListener(OnClickGiftListListener onClickGiftListListener) {
        this.onClickGiftListListener = onClickGiftListListener;
    }

    public void setGiftListData(List<GiftInfo> giftList) {
        giftListAdapter.updateData(giftList);
        giftListAdapter.notifyDataSetChanged();
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE){
            et_num.setText("1");
        }
    }
}
