package com.bc_chat.im.activity.envelopes;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bc_chat.bc_base.contract.SendRedPacketContract;
import com.bc_chat.bc_base.entity.ListModel;
import com.bc_chat.bc_base.entity.group.MemberBean;
import com.bc_chat.bc_base.entity.wallet.SendRedPacketEntity;
import com.bc_chat.bc_base.presenter.SendRedPacketPresenter;
import com.bc_chat.im.R;
import com.jrmf360.rylib.common.util.f;
import com.jrmf360.rylib.common.util.q;
import com.jrmf360.rylib.wallet.e.a;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.utils.Preferences;

import java.math.BigDecimal;

public class SendGroupEnvelopesActivity extends BaseActivity<SendRedPacketPresenter> implements SendRedPacketContract.View {

    private int ENVELOPES_TYPE = 1;
    private TextView pop_message;
    private LinearLayout ll_peak_num_layout;
    private EditText et_peak_num;
    private EditText et_amount;
    private EditText et_peak_message;
    private LinearLayout ll_peak_amount_layout;
    private TextView tv_amount_for_show;
    private Button btn_putin;
    private int NUMBER = -1;
    private double AMOUNTMONEY = -1.0D;
    protected int maxCount = 100;
    protected String maxLimitMoney = "999";
    private String currentMoney;
    private String currentPeakMessage;
    private String currentCount;
    private String targetId;

    @Override
    public int getLayoutResId() {
        return R.layout._activity_send_group_peak2;
    }

    @Override
    protected void initialize() {
        initView();
        initListener();
        initData();
    }

    private void initData() {
        targetId = getIntent().getStringExtra("TargetId");
//        getPresenter().groupinfoTask(targetId);
    }

    private void initListener() {
        this.et_peak_num.addTextChangedListener(new a() {
            public void afterTextChanged(Editable var1) {
                NUMBER = checkNum();
                if (NUMBER != -1) {
                    AMOUNTMONEY = checkAmount();
                }
                checkForAllAmount();
                checkForButton();
            }
        });
        this.et_amount.addTextChangedListener(new a() {
            public void afterTextChanged(Editable var1) {
                NUMBER = checkNum();
                if (NUMBER != -1) {
                    AMOUNTMONEY = checkAmount();
                }
                checkForAllAmount();
                checkForButton();
            }
        });
        et_amount.setFilters(new InputFilter[]{new InputMoney()});
        btn_putin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentMoney = et_amount.getText().toString().trim();
                currentPeakMessage = et_peak_message.getText().toString().trim();
                currentCount = et_peak_num.getText().toString().trim();
                if (Preferences.getIsSetPayPwd()) {
                    showPayDialog();
                } else {
                    ModifyPayPasswordActivity.start(SendGroupEnvelopesActivity.this, ModifyPayPasswordActivity.REQUEST_CODE_SET);
                }
            }
        });
    }

    public void showPayDialog() {
        PayPasswordVerifyDialog dialog = new PayPasswordVerifyDialog(SendGroupEnvelopesActivity.this);
        dialog.setAction("红包");
        dialog.setMoney(currentMoney);
        dialog.setOnInputFinishListener(password -> {
            doSendRedPacket(password);
        });
        dialog.show();
    }

    private void doSendRedPacket(String password) {
        getPresenter().sendRedPacket(2, "", currentMoney, password, targetId, Integer.parseInt(currentCount), currentPeakMessage);
    }

    private void initView() {
        pop_message = (TextView) this.findViewById(R.id.pop_message);
        ll_peak_num_layout = (LinearLayout) this.findViewById(R.id.ll_peak_num_layout);
        et_peak_num = (EditText) this.findViewById(R.id.et_peak_num);
        et_peak_num.setFocusable(true);
        et_peak_num.setFocusableInTouchMode(true);
        et_peak_num.requestFocus();
        et_peak_num.setSelection(this.et_peak_num.getText().length());
        et_peak_num.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
        ll_peak_amount_layout = (LinearLayout) this.findViewById(R.id.ll_peak_amount_layout);
        et_amount = (EditText) this.findViewById(R.id.et_peak_amount);
        et_peak_message = (EditText) this.findViewById(R.id.et_peak_message);
        tv_amount_for_show = (TextView) this.findViewById(R.id.tv_amount_for_show);
        btn_putin = (Button) this.findViewById(R.id.btn_putin);
        setDefaultView();

    }

    private void setDefaultView() {
        this.pop_message.setVisibility(View.GONE);
        this.pop_message.getBackground().mutate().setAlpha(80);
        this.tv_amount_for_show.setText("0.00");
    }

    @Override
    protected SendRedPacketPresenter initPresenter() {
        return new SendRedPacketPresenter<>(this);
    }

    public static boolean isNumber(String var0) {
        return !q.a(var0) ? var0.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$") : false;
    }

    private int checkNum() {
        String var1 = this.et_peak_num.getText().toString();
        if (!q.a(var1)) {
            if (isNumber(var1)) {
                BigDecimal var2 = new BigDecimal(var1);
                int var3 = var2.intValue();
                if (var3 == 0) {
                    this.showTips(this.getString(R.string.jrmf_rp_min_num));
                    this.ll_peak_num_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                    return -1;
                } else if (var3 > this.maxCount) {
                    this.showTips(String.format(this.getString(R.string.jrmf_rp_max_num), this.maxCount));
                    this.ll_peak_num_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                    return -1;
                } else {
                    this.ll_peak_num_layout.setBackgroundResource(R.drawable._bg_white_round);
                    this.closeTips();
                    return var3;
                }
            } else {
                this.showTips(this.getString(R.string.jrmf_rp_num_error));
                this.ll_peak_num_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                return -1;
            }
        } else {
            this.closeTips();
            this.ll_peak_num_layout.setBackgroundResource(R.drawable._bg_white_round);
            return -1;
        }
    }

    private double checkAmount() {
        String var1 = this.et_amount.getText().toString();
        if (!q.a(var1)) {
            if (!var1.startsWith(".")) {
                BigDecimal var2 = new BigDecimal(var1);
                double var3 = var2.doubleValue();
                if (var3 == 0.0) {
                    this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                    this.closeTips();
                    return -1.0;
                } else if (var3 < 0.01) {
                    this.showTips(this.getString(R.string.jrmf_rp_min_amount));
                    this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                    return -1.0;
                } else if (this.ENVELOPES_TYPE == 1) {
                    if (var3 > q.j(this.maxLimitMoney)) {
                        this.showTips(String.format(this.getString(R.string.jrmf_rp_max_amount), this.maxLimitMoney));
                        this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                        return -1.0;
                    } else {
                        this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                        this.closeTips();
                        return var3;
                    }
                } else if (this.ENVELOPES_TYPE == 0) {
                    String var5 = this.et_peak_num.getText().toString();
                    if (var5 != null && !var5.isEmpty()) {
                        double var6 = q.i(var5);
                        if (var6 != 0.0 && var6 * var3 > q.i(this.maxLimitMoney)) {
                            this.showTips(String.format(this.getString(R.string.jrmf_rp_max_amount), this.maxLimitMoney));
                            this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                            return -1.0;
                        } else {
                            this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                            this.closeTips();
                            return var3;
                        }
                    } else if (var3 > q.i(this.maxLimitMoney)) {
                        this.showTips(String.format(this.getString(R.string.jrmf_rp_max_amount), this.maxLimitMoney));
                        this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                        return -1.0;
                    } else {
                        this.closeTips();
                        this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                        return var3;
                    }
                } else {
                    return -1.0;
                }
            } else {
                this.showTips(this.getString(R.string.jrmf_rp_amount_error));
                this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                return -1.0;
            }
        } else {
            this.closeTips();
            this.ll_peak_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
            return -1.0;
        }
    }

    private void showTips(String var1) {
        this.pop_message.setText(var1);
        this.pop_message.setVisibility(View.VISIBLE);
    }

    private void closeTips() {
        this.pop_message.setText("");
        this.pop_message.setVisibility(View.GONE);
    }

    private void checkForAllAmount() {
        int var1 = -1;
        String var2 = this.et_peak_num.getText().toString();
        if (!q.a(var2) && isNumber(var2)) {
            BigDecimal var3 = new BigDecimal(var2);
            var1 = var3.intValue();
        }

        float var7 = 0.0F;
        String var4 = this.et_amount.getText().toString();
        BigDecimal var5;
        if (!q.a(var4) && !var4.startsWith(".")) {
            var5 = new BigDecimal(var4);
            var7 = var5.floatValue();
        }

        if (var1 > 0 && var7 > 0.0F) {
            if (this.ENVELOPES_TYPE == 1) {
                var5 = new BigDecimal((double) var7);
                var5 = var5.setScale(2, 5);
                this.tv_amount_for_show.setText(var5 + "");
            } else if (this.ENVELOPES_TYPE == 0) {
                var5 = new BigDecimal((double) var7);
                BigDecimal var6 = var5.multiply(new BigDecimal(var1));
                var6 = var6.setScale(2, 5);
                this.tv_amount_for_show.setText(var6 + "");
            }
        } else {
            this.tv_amount_for_show.setText("0.00");
        }

    }

    private void checkForButton() {
        f.a(this.btn_putin, false);
        if (this.NUMBER > 0 && this.AMOUNTMONEY > 0.0) {
            f.a(this.btn_putin, true);
        }

    }

    @Override
    public void sendRedPacketSuccess(@org.jetbrains.annotations.Nullable SendRedPacketEntity sendRedPacketEntity) {
        if (sendRedPacketEntity != null) {
            Intent data = new Intent();
            data.putExtra("envelopesID", sendRedPacketEntity.getPacket_id());
            data.putExtra("envelopeMessage", currentPeakMessage);
            data.putExtra("envelopeName", "群红包");
            setResult(RESULT_OK, data);
            finish();
        }
    }

    @Override
    public void sendRedPacketFailure(@org.jetbrains.annotations.Nullable Exception exception) {

    }

    @Override
    public void queryGroupMemberSuccess(@org.jetbrains.annotations.Nullable ListModel<MemberBean> memberModel) {
        this.maxCount = memberModel.getList().size();
    }

    protected class InputMoney implements InputFilter {
        protected InputMoney() {
        }

        public CharSequence filter(CharSequence var1, int var2, int var3, Spanned var4, int var5, int var6) {
            if (var1.toString().equals(".") && var5 == 0 && var6 == 0) {
                et_amount.setText("0" + var1 + var4);
                et_amount.setSelection(2);
            }

            if (var5 >= 8) {
                return "";
            } else {
                return var4.toString().indexOf(".") != -1 && var4.length() - var4.toString().indexOf(".") > 2 && var4.length() - var5 < 3 ? "" : null;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ModifyPayPasswordActivity.REQUEST_CODE_SET) {
                showPayDialog();
            }
        }
    }

}
