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.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.i;
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 SendSingleEnvelopesActivity extends BaseActivity<SendRedPacketPresenter> implements SendRedPacketContract.View {

    private TextView pop_message;
    private LinearLayout ll_amount_layout;
    private TextView tv_amount;
    private Button btn_putin;
    private EditText et_amount;
    private EditText et_peak_message;
    protected String maxLimitMoney = "200";
    private String targetId;
    private String currentMoney;
    private String currentPeakMessage;

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

    @Override
    protected void initialize() {
        initView();

        initData();
    }

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

    private void initView() {
        this.pop_message = (TextView) this.findViewById(R.id.pop_message);
        this.pop_message.getBackground().mutate().setAlpha(80);
        this.pop_message.setVisibility(View.GONE);
        this.ll_amount_layout = (LinearLayout) this.findViewById(R.id.ll_amount_layout);
        this.et_amount = (EditText) this.findViewById(R.id.et_amount);
        this.et_peak_message = (EditText) this.findViewById(R.id.et_peak_message);
        this.tv_amount = (TextView) this.findViewById(R.id.tv_amount);
        this.tv_amount.setText("0.00");
        this.btn_putin = (Button) this.findViewById(R.id.btn_putin);

        initListener();
    }

    private void initListener() {
        this.et_amount.addTextChangedListener(new a() {
            public void afterTextChanged(Editable var1) {
                super.afterTextChanged(var1);
                checkAmount();
            }
        });
        this.et_amount.setFilters(new InputFilter[]{new InputMoney()});
        this.btn_putin.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                currentMoney = tv_amount.getText().toString().trim();
                currentPeakMessage = et_peak_message.getText().toString().trim();
                if (Preferences.getIsSetPayPwd()) {
                    showPayDialog();
                } else {
                    ModifyPayPasswordActivity.start(SendSingleEnvelopesActivity.this, ModifyPayPasswordActivity.REQUEST_CODE_SET);
                }
            }
        });
    }

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

    private void doSendRedPacket(String password) {
        getPresenter().sendRedPacket(1, targetId, currentMoney, password, "", 1, currentPeakMessage);
    }

    private void 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.closeTips();
                    this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                } else if (var3 < 0.01) {
                    this.showTips(this.getString(R.string.jrmf_rp_min_amount));
                    this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                    f.a(this.btn_putin, false);
                } else if (var3 > q.i(this.maxLimitMoney)) {
                    this.showTips(String.format(this.getString(R.string.jrmf_rp_single_max_amount), this.maxLimitMoney));
                    this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                    f.a(this.btn_putin, false);
                } else {
                    this.closeTips();
                    this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
                    f.a(this.btn_putin, true);
                }

                if (var3 > 0.0) {
                    var2 = var2.setScale(2, 4);
                    this.tv_amount.setText("" + var2);
                } else {
                    this.tv_amount.setText("0.00");
                }
            } else {
                this.showTips(this.getString(R.string.jrmf_rp_amount_error));
                this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round_stroke);
                f.a(this.btn_putin, false);
            }
        } else {
            this.closeTips();
            f.a(this.btn_putin, false);
            this.ll_amount_layout.setBackgroundResource(R.drawable._bg_white_round);
            this.tv_amount.setText("0.00");
        }

    }

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

    @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) {
        
    }

    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) {
                SendSingleEnvelopesActivity.this.et_amount.setText("0" + var1 + var4);
                SendSingleEnvelopesActivity.this.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;
            }
        }
    }

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

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


    public void onBackPressed() {
        super.onBackPressed();
        i.b(this);
    }

    @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();
            }
        }
    }
}
