package com.navinfo.outdoor.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.util.DialogSettings;
import com.kongzue.dialog.v3.MessageDialog;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.FragmentManagement;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.GetPriceBean;
import com.navinfo.outdoor.bean.UserPriceExchangeBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;

/**
 * 提现页面的fragment
 */
public class WithdrawFragment extends BaseFragment implements View.OnClickListener {

    private ImageView ivWithDraw;
    private TextView tvText;
    private TextView tvAll, tvUnit, tvAlready, tvTotal;
    private Button btWithdraw;
    private double canExchangePrice;
    private EditText etAllPrice;
    private TextView poiPushMoney;
    private TextView poiReportMoney;
    private TextView poiVideoPushMoney;
    private TextView poiVideoReportMoney;
    private TextView roadPushMoney;
    private TextView roadReportMoney;
    private TextView chargingPushMoney;
    private TextView chargingReportMoney;
    private TextView otherPushMoney;
    private TextView otherReportMoney;
    private CheckBox checkBox;

    public static WithdrawFragment newInstance(Bundle bundle) {
        WithdrawFragment fragment = new WithdrawFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.withdraw_fragment;
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initView() {
        super.initView();
        tvUnit = findViewById(R.id.tv_unit);//可提现
        tvAlready = findViewById(R.id.tv_already);//已提现
        tvTotal = findViewById(R.id.tv_total);//总资产
        tvAll = findViewById(R.id.tv_all);
        tvAll.setOnClickListener(this::onClick);
        etAllPrice = findViewById(R.id.et_allPrice);
        ivWithDraw = findViewById(R.id.iv_withdraw);
        ivWithDraw.setOnClickListener(this);
        tvText = findViewById(R.id.tv_text);
        tvText.setOnClickListener(this::onClick);
        checkBox = findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (Constant.ID_NUMBER==null||Constant.BANKACCOUNT==null){
                        Toast.makeText(getActivity(), "没有绑定银行卡，请先绑定银行卡", Toast.LENGTH_SHORT).show();
                        checkBox.setChecked(false);
                    }else {
                        checkBox.setChecked(true);
                    }
                }
            }
        });
        btWithdraw = findViewById(R.id.button);
        btWithdraw.setOnClickListener(this::onClick);
        poiPushMoney = (TextView) findViewById(R.id.poi_push_money);
        poiReportMoney = (TextView) findViewById(R.id.poi_report_money);
        poiVideoPushMoney = (TextView) findViewById(R.id.poiVideo_push_money);
        poiVideoReportMoney = (TextView) findViewById(R.id.poiVideo_report_money);
        roadPushMoney = (TextView) findViewById(R.id.road_push_money);
        roadReportMoney = (TextView) findViewById(R.id.road_report_money);
        chargingPushMoney = (TextView) findViewById(R.id.charging_push_money);
        chargingReportMoney = (TextView) findViewById(R.id.charging_report_money);
        otherPushMoney = (TextView) findViewById(R.id.other_push_money);
        otherReportMoney = (TextView) findViewById(R.id.other_report_money);

        initNetWork();

    }

    private void initNetWork() {
        showLoadingDialog();
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.GET_PRICE)
                .cls(GetPriceBean.class)
                .token(Constant.ACCESS_TOKEN)
                .params(new HttpParams())
                .getRequest(new Callback<GetPriceBean>() {
                    @Override
                    public void onSuccess(GetPriceBean response, int id) {
                        dismissLoadingDialog();
                        //  Toast.makeText(getContext(), "获取成功", Toast.LENGTH_SHORT).show();
                       if (response.getCode()==200){
                           Double userPrice = response.getBody().getUserPrice();
                           canExchangePrice = response.getBody().getCanExchangePrice();
                           tvTotal.setText(userPrice + "");
                           tvUnit.setText(canExchangePrice + "");
                           tvAlready.setText(response.getBody().getHaveExchangePrice() + "");
                           poiPushMoney.setText(response.getBody().getPoiPushPrice() + "元");
                           poiReportMoney.setText(response.getBody().getPoiNonepushPrice() + "元");
                           poiVideoPushMoney.setText(response.getBody().getPoivideoPushPrice() + "元");
                           poiVideoReportMoney.setText(response.getBody().getPoivideoNonepushPrice() + "元");
                           roadPushMoney.setText(response.getBody().getRoadPushPrice() + "元");
                           roadReportMoney.setText(response.getBody().getRoadNonepushPrice() + "元");
                           chargingPushMoney.setText(response.getBody().getCsPushPrice() + "元");
                           chargingReportMoney.setText(response.getBody().getCsNonepushPrice() + "元");
                           otherPushMoney.setText(response.getBody().getOtherPushPrice() + "元");
                           otherReportMoney.setText(response.getBody().getOtherNonepushPrice() + "元");
                       }else {
                           Toast.makeText(getActivity(), response.getMessage()+"", Toast.LENGTH_SHORT).show();
                       }

                        Log.d("TAG", "onSuccess: " + response.toString() + "tttttttt");
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        //  Toast.makeText(getContext(), "获取失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_withdraw:
                getActivity().finish();
                break;
            case R.id.tv_text:
                Intent intentText = new Intent(getActivity(), FragmentManagement.class);
                intentText.putExtra("tag", 26);
                startActivity(intentText);
                break;

            case R.id.button:
                String price = etAllPrice.getText().toString().trim();
                if (price!=null&&!price.equals("")){
                    Integer integer = Integer.valueOf(price);
                    if (integer!=null&&integer<10){
                        Toast.makeText(getActivity(), "提现金额不能低于10元", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }else {
                    Toast.makeText(getActivity(), "提现金额不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!checkBox.isChecked()) {
                    Toast.makeText(getActivity(), "请勾选银行卡", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Constant.ID_NUMBER != null) {
                    DialogSettings.style = DialogSettings.STYLE.STYLE_KONGZUE;
                    MessageDialog.show((AppCompatActivity) getContext(), "提示", "是否提取到" + Constant.ID_NUMBER.substring(Constant.ID_NUMBER.length() - 4) + "的银行卡", "确定", "取消").setOkButton(new OnDialogButtonClickListener() {
                        @Override
                        public boolean onClick(BaseDialog baseDialog, View v) {
                            immediatelyPrice();
                            return false;
                        }
                    });
                } else {
                    Toast.makeText(getActivity(), "请先绑定银行卡", Toast.LENGTH_SHORT).show();
                    return;
                }


                break;
            case R.id.tv_all:
                etAllPrice.setText(canExchangePrice + "");
                break;
        }
    }

    private void immediatelyPrice() {
        showLoadingDialog();
        setLoadingDialogText("提现中...");
        HttpParams httpParams = new HttpParams();

        httpParams.put("exchangeMoney", etAllPrice.getText().toString());
        httpParams.put("payType", "1");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.USER_PRICE_EXCHANGE)
                .cls(UserPriceExchangeBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<UserPriceExchangeBean>() {
                    @Override
                    public void onSuccess(UserPriceExchangeBean response, int id) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), response.getMessage(), Toast.LENGTH_SHORT).show();
                        if (response.getCode() == 200) {
                            String unit = tvUnit.getText().toString();
                            String price = etAllPrice.getText().toString();
                            double v = Double.valueOf(unit) - Double.valueOf(price);
                            String s = String.format("%.2f", v).toString();
                            tvUnit.setText(s);
                        }else {
                            Toast.makeText(getActivity(), response.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }
                        etAllPrice.getText().clear();

                        Log.d("TAG", "onSuccess: " + response.toString() + "tttttttt");
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }
}
