package com.hw.nativeapp.ui.activity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.ErrorConsumer;
import com.hw.nativeapp.httpnet.ResponseConsumer;
import com.hw.nativeapp.utils.ActivityUtils;
import com.hw.nativeapp.utils.MaskUtil;
import com.hw.nativeapp.utils.RxUtil;

import butterknife.BindView;
import butterknife.OnClick;


public class InfoInputActivity extends BaseActivity  {

    public static final Integer COUNTDOWN_TIME = 120000;
    private CountDownTimer timer;
    private TitleBar titleBar;
    private ProgressDialog progressDialog;

    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_idcard)
    EditText et_idcard;

    private String performId = "";
    private String auditoriumId = "";
    private String date = "";
    private String goodsId = "";
    private String salePeice = "";
    private String performTimeId = "";
    private String seatTypeId = "";
    private String name = "";
    private String idcard = "";
    private String type = "";  // 1 是购票人  2是观影人
    private JSONArray viewerList = new JSONArray();
    @Override
    void initView() {
        titleBar = findViewById(R.id.tb_main_bar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {

            @Override
            public void onLeftClick(View v) {
                ActivityUtils.activityFinish(InfoInputActivity.this, true );
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        getPageTransmitParams();
        startCountDownTimer();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_input);
    }

    @OnClick({R.id.input_btn})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.input_btn:
                factorAuth();
                break;
        }
    }
    //获取页面传递参数
    private void getPageTransmitParams(){
        Intent transmitIntent = getIntent();
        performId = transmitIntent.getStringExtra("performId");
        auditoriumId = transmitIntent.getStringExtra("auditoriumId");
        goodsId = transmitIntent.getStringExtra("goodsId");
        salePeice = transmitIntent.getStringExtra("salePeice");
        performTimeId = transmitIntent.getStringExtra("performTimeId");
        seatTypeId = transmitIntent.getStringExtra("seatTypeId");
        type = transmitIntent.getStringExtra("type");
        if(type.equals("2")){
            name = transmitIntent.getStringExtra("name");
            idcard = transmitIntent.getStringExtra("idcard");
        }
        viewerList = JSONArray.parseArray(transmitIntent.getStringExtra("viewerList"));
    }
    private void jumpToLogin(){
        stopCountDownTimer();
        ActivityUtils.activityFinish(InfoInputActivity.this, true);
    }
    private void stopCountDownTimer(){
        if (timer != null){
            timer.cancel();
        }
    }
    private void startCountDownTimer(){
        timer = new CountDownTimer(COUNTDOWN_TIME, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                titleBar.setRightTitle(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                jumpToLogin();
            }
        };
        timer.start();
    }

    // 二要数认证
    private void factorAuth(){
        if(et_name.getText().toString().isEmpty() || et_idcard.getText().toString().isEmpty()){
            ToastUtils.showShort("请输入姓名和身份证号");
            return;
        }
        if(et_name.getText().toString().length() < 2 || et_idcard.getText().toString().length() < 18){
            ToastUtils.showShort("姓名和身份证号格式错误");
            return;
        }
        JSONObject options = new  JSONObject();
        options.put("name", et_name.getText().toString());
        options.put("idcard", et_idcard.getText().toString());
        progressDialog = MaskUtil.showProgressDialog( "正在验证...", InfoInputActivity.this);
        new Handler().postDelayed(() -> {
            if(progressDialog != null){
                progressDialog.dismiss();
            }
        }, 30000);
        MApplication.getApiService().factorAuth( options, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) {
                        if(data.getIntValue("status") == 1) {
                            if(progressDialog != null){
                                progressDialog.dismiss();
                            }
                            successFu();
                        } else {
                            if(progressDialog != null){
                                progressDialog.dismiss();
                            }
                            ToastUtils.showShort(data.getString("errReason"));
                        }
                    }

                    @Override
                    public void onFailed(int code, String msg) {
                        if(progressDialog != null){
                            progressDialog.dismiss();
                        }
                    }
                }, new ErrorConsumer());
    }

    private void successFu () {
        Bundle bundle = new Bundle();
        bundle.putString("performId", performId);
        bundle.putString("auditoriumId", auditoriumId);
        bundle.putString("performTimeId", performTimeId);
        bundle.putString("seatTypeId", seatTypeId);
        bundle.putString("goodsId", goodsId);
        bundle.putString("salePeice", salePeice);
        bundle.putString("type", "1");
        if(type.equals("1")){
            bundle.putString("name", et_name.getText().toString());
            bundle.putString("idcard", et_idcard.getText().toString());
        } else {
            bundle.putString("name", name);
            bundle.putString("idcard", idcard);
        }
        if(viewerList == null || viewerList.size() == 0){
            viewerList =  new JSONArray();
        }
        JSONObject obj = new JSONObject();
        obj.put("name", et_name.getText().toString());
        obj.put("idcard", et_idcard.getText().toString());
        viewerList.add(obj);
        bundle.putString("viewerList", JSONArray.toJSONString(viewerList));
        ActivityUtils.launchActivity(this,PurchaseInfoActivity.class, bundle);
    }
}