package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogRenewBinding;
import com.example.iot_controlhost.model.net.CommonResponse;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.network.Url;
import com.example.iot_controlhost.utils.network.XHttpManager;
import com.example.iot_controlhost.utils.network.XHttpShorthand;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Random;

/**
 * 续费弹窗
 */
public class RenewDialog extends BaseDialog<DialogRenewBinding> {

    private final String[] packages = new String[]{"测试1", "测试2", "测试3", "测试4"};
    private final String[] times = new String[]{"测试1", "测试2", "测试3", "测试4"};

    /**
     * 选择的套餐类型
     */
    String selPackage = null;
    /**
     * 选择的续费时长
     */
    String selTime = null;
    /**
     * 选择的支付类型
     * 0微信 1支付宝
     */
    int selPayType = 0;

    public RenewDialog(Context context) {
        super(context);
        binding.acInputPackage.setHint("流量套餐");
        binding.acInputTime.setHint("续费时长");
        binding.acInputPackage.setAdapter(new CustomSpinnerAdapter(mContext, packages));
        binding.acInputTime.setAdapter(new CustomSpinnerAdapter(mContext, times));
        binding.acInputTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                selTime = s.toString();
                loadImage();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.acInputPackage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                selPackage = s.toString();
                loadImage();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 读取支付码-网络请求
     * 根据套餐类型、续费时长与支付类型获取支付码`
     */
    private void loadImage() {
        if (selPackage == null) {
            infoTips("请选择套餐类型");
        } else if (selTime == null) {
            infoTips("请选择续费时长");
        } else {
            renew();
        }
        binding.ivAli.setOnClickListener(v -> {
            selPayType = 1;
            binding.ivAli.setImageDrawable(mContext.getDrawable(R.mipmap.sel));
            binding.ivWe.setImageDrawable(mContext.getDrawable(R.mipmap.unsel));
            loadImage();
        });
        binding.ivWe.setOnClickListener(v -> {
            selPayType = 2;
            binding.ivWe.setImageDrawable(mContext.getDrawable(R.mipmap.sel));
            binding.ivAli.setImageDrawable(mContext.getDrawable(R.mipmap.unsel));
            loadImage();
        });
    }

    private void renew() {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.renew);
        params.addParameter("areaCode", RoomSetting.getAreaCode());
        x.http().post(params, new XHttpShorthand<CommonResponse>() {
            @Override
            public void success(CommonResponse model) {
                MyLog.app("续费图像信息获取成功");
                binding.ivPay.setImageDrawable(mContext.getDrawable(R.mipmap.logo_gys));
                binding.tvPrice.setText(String.valueOf(new Random().nextInt(2000)));
                binding.tvOriginPrice.setText(Html.fromHtml("<s>" + (2000 + new Random().nextInt(50000)) + "</s>"));
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("续费图像信息获取失败");
            }
        });
    }

}
