package com.hw.nativeapp.ui.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hw.nativeapp.R;
import com.hw.nativeapp.utils.ActivityUtils;

import butterknife.BindView;
import butterknife.OnClick;


public class ScanCodePayActivity extends BaseActivity  {

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

    @BindView(R.id.im_pay_result)
    ImageView im_pay_result;
    @BindView(R.id.tx_pay_result)
    TextView tx_pay_result;
    @BindView(R.id.tx_pay_result_money)
    TextView tx_pay_result_money;
    @BindView(R.id.code_btn)
    TextView code_btn;

    private String orderId = "";
    private String idcard = "";
    private String payStatus = ""; //支付状态: 0-未支付 1-已支付 2-支付中 3-支付失败 4-支付退款
    private String total = "";
    private String totalPeice = "";
    private String payAmount = "";
//    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(ScanCodePayActivity.this, true );
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

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

    @OnClick({R.id.code_btn})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.code_btn:
                Bundle bundle = new Bundle();
                bundle.putString("idcard", idcard);
                ActivityUtils.launchActivity(this, TicketCollectionActivity.class, bundle);
                break;
        }
    }

    //获取页面传递参数
    private void getPageTransmitParams(){
        Intent transmitIntent = getIntent();
        orderId = transmitIntent.getStringExtra("orderId");
        idcard = transmitIntent.getStringExtra("idcard");
        payStatus = transmitIntent.getStringExtra("payStatus");
        total = transmitIntent.getStringExtra("total");
        totalPeice = transmitIntent.getStringExtra("totalPeice");
        payAmount = transmitIntent.getStringExtra("payAmount");
//        viewerList = JSONArray.parseArray(transmitIntent.getStringExtra("viewerList"));
    }

    private void initPageView(){
        if (payStatus.equals("1")){
            im_pay_result.setBackground(getResources().getDrawable(R.mipmap.code_ok));
            tx_pay_result.setText("支付成功");
            code_btn.setVisibility(View.VISIBLE);
        } else if (payStatus.equals("3")) {
            im_pay_result.setBackground(getResources().getDrawable(R.mipmap.code_not));
            tx_pay_result.setText("支付失败");
            code_btn.setVisibility(View.GONE);
        }
        tx_pay_result_money.setText("共" + total + "张，合计¥" + payAmount);
    }

    private void jumpToLogin(){
        stopCountDownTimer();
        ActivityUtils.activityFinish(ScanCodePayActivity.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();
    }
}