package com.chinapnr.pos.pay.demo.trans;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.chinapnr.pos.config.ChannelId;
import com.chinapnr.pos.config.InterfaceType;
import com.chinapnr.pos.config.PnrRequestKey;
import com.chinapnr.pos.config.PnrResponseKey;
import com.chinapnr.pos.config.key.trans.VoidRequestKey;
import com.chinapnr.pos.pay.demo.R;
import com.chinapnr.pos.trans.PnrService;
import com.chinapnr.pos.trans.PnrTransListener;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import timber.log.Timber;

public class VoidActivity extends AppCompatActivity {


    TextView tvRequest, tvResult;
    EditText etMemberId, merOperId,merOperIdPrinted, etOriVoucher, etOrdNo;
    Button btnAcquire;
    Button btnScanW;
    Button btnScanA;
    RadioGroup radioGroup;
    RadioButton qrcodeU,qrcodeJ,qrcodeW;
    String payType;

    EditText printKey1,printValue1,printKey2,printValue2,printKey3,printValue3;
    EditText etCashOutMode;
    private Map<String,Object> outPrintData;
    private View view_line;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_void);

        btnAcquire = (Button) findViewById(R.id.btn_acquire);
        btnScanW = (Button)findViewById(R.id.btn_scan_w);
        btnScanA = (Button)findViewById(R.id.btn_scan_a);

        tvRequest = (TextView) findViewById(R.id.tv_request);
        tvResult = (TextView) findViewById(R.id.tv_result);
        etMemberId = (EditText) findViewById(R.id.et_member_id);
        merOperId = (EditText) findViewById(R.id.et_merOperId);
        merOperIdPrinted = (EditText) findViewById(R.id.et_merOperId_printed);
        etOriVoucher = (EditText) findViewById(R.id.et_ori_voucher);
        etOrdNo = (EditText) findViewById(R.id.et_ord_no);
        //获取实例
        radioGroup=(RadioGroup)findViewById(R.id.radioGroupID);
        qrcodeU=(RadioButton)findViewById(R.id.qrcode_u);
        qrcodeJ=(RadioButton)findViewById(R.id.qrcode_j);
        qrcodeW=(RadioButton)findViewById(R.id.qrcode_w);

        //获取自定义打印数据
        printKey1 = (EditText)findViewById(R.id.printKey1) ;
        printKey2 = (EditText)findViewById(R.id.printKey2) ;
        printKey3 = (EditText)findViewById(R.id.printKey3) ;
        printValue1 = (EditText)findViewById(R.id.printValur1) ;
        printValue2 = (EditText)findViewById(R.id.printValur2) ;
        printValue3 = (EditText)findViewById(R.id.printValur3) ;
        view_line = findViewById(R.id.view_line) ;

        etCashOutMode = (EditText)findViewById(R.id.et_cashOutMode);
        updateOrdNo();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {
                if (checkedId==qrcodeU.getId()){
                    System.out.println("选中了银联钱包!");
                    payType = "U";
                }else if (checkedId==qrcodeJ.getId()){
                    System.out.println("选中了支付宝!");
                    payType = "A";
                }else if (checkedId==qrcodeW.getId()){
                    System.out.println("选中了微信!");
                    payType = "W";
                }
            } } );
        btnAcquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doVoid(ChannelId.CHANNEL_BANK);
            }
        });

        btnScanA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doVoid(ChannelId.CHANNEL_SCAN);
            }
        });
        btnScanW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doVoid(ChannelId.CHANNEL_QR_CODE);
            }
        });
    }

    private void doVoid(String channelId){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(VoidRequestKey.KEY_CHANNEL_ID, channelId);
        jsonObject.addProperty(VoidRequestKey.KEY_MEMBER_ID, etMemberId.getText().toString());
        jsonObject.addProperty(VoidRequestKey.KEY_MER_OPER_ID, merOperId.getText().toString());
        jsonObject.addProperty(VoidRequestKey.KEY_MER_OPER_ID_PRINTED, merOperIdPrinted.getText().toString());
        jsonObject.addProperty(VoidRequestKey.KEY_OUT_ORD_ID, etOrdNo.getText().toString());
        jsonObject.addProperty(VoidRequestKey.KEY_ORI_VOUCHER_NO, etOriVoucher.getText().toString());
//        jsonObject.addProperty(VoidRequestKey.KEY_CASH_OUT_MODE, etCashOutMode.getText().toString());
        jsonObject.addProperty(VoidRequestKey.KEY_BG_RET_URL, "https://www.baidu.com/");
        if (isPrintOutData()){
            jsonObject.addProperty(VoidRequestKey.KEY_OUT_PRINT_DATA, new Gson().toJson(getPrintData()));
        }
        tvRequest.setText("接口:" + InterfaceType.Trans.IT_VOID + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);

        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Trans.IT_VOID);
        PnrService.getInstance(getApplicationContext()).doTrans(jsonObject.toString(), new PnrTransListener() {
            @Override
            public void onResult(String result) {
                Timber.e(result);
                Map resultMap = new Gson().fromJson(result, Map.class);
                String responseCode = resultMap.get(PnrResponseKey.KEY_RESPONSECODE).toString();
                if ("00".equals(responseCode)){
                    tvResult.setText("撤销成功："+result);
                }else {
                    tvResult.setText("撤销失败：" + result);
                }
            }

        });

    }

    private boolean isPrintOutData(){
        return !TextUtils.isEmpty(printKey1.getText().toString()) || !TextUtils.isEmpty(printKey2.getText().toString()) || !TextUtils.isEmpty(printKey3.getText().toString());
    }

    private Map<String,Object> getPrintData(){
        outPrintData = new HashMap<String,Object>();
        if(printKey1.getText().toString()!=null && printKey1.getText().toString().length()>0){
            outPrintData.put(printKey1.getText().toString(),printValue1.getText().toString());
        }
        if(printKey2.getText().toString()!=null && printKey2.getText().toString().length()>0){
            outPrintData.put(printKey2.getText().toString(),printValue2.getText().toString());
        }
        if(printKey3.getText().toString()!=null && printKey3.getText().toString().length()>0){
            outPrintData.put(printKey3.getText().toString(),printValue3.getText().toString());
        }
        return outPrintData;
    }

    private void updateOrdNo() {
        etOrdNo.setText(getMerOrdNo());
    }



    public String getMerOrdNo() {
        String traceNo = (new Random().nextInt(999999)) + "";
        String date = (String) DateFormat.format("yyyyMMddHHmmss", Calendar.getInstance(Locale.CHINA));
        return date + traceNo;
    }
}