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

import android.os.Bundle;
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.RefundRequestKey;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import timber.log.Timber;

public class RefundActivity extends AppCompatActivity {

    Button btnComfirm;
    TextView tvRequest, tvResult;
    EditText etMemberId,  merOperId,merOperIdPrinted, etOrdNo ,etOriOrdNo ,etAmt,etOriOutOrdNo;
    EditText etCashOutMode ,etMemberId1 ,etUndertakeAmt1,etConfirmTermOrdId1,etMemberId2 ,etUndertakeAmt2,etConfirmTermOrdId2,etMemberId3 ,etUndertakeAmt3,etConfirmTermOrdId3;
    RadioGroup radioGroup;
    RadioButton btnAquire, btnScan;
    String channelId;
    private View view_line;

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

        btnComfirm = (Button) findViewById(R.id.btn_confirm);

        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);
        etOriOrdNo = (EditText) findViewById(R.id.et_ori_ord_no);
        etOriOutOrdNo = (EditText) findViewById(R.id.et_ori_out_ord_no);
        etOrdNo = (EditText) findViewById(R.id.et_ord_no);
        etAmt = (EditText) findViewById(R.id.et_amount);

        //退货串

        etCashOutMode = (EditText)findViewById(R.id.et_cashOutMode);

        etMemberId1 = (EditText)findViewById(R.id.et_memberId1);
        etUndertakeAmt1 = (EditText)findViewById(R.id.et_undertakeAmt1);
        etConfirmTermOrdId1 = (EditText)findViewById(R.id.et_confirmTermOrdId1);

        etMemberId2 = (EditText)findViewById(R.id.et_memberId2);
        etUndertakeAmt2 = (EditText)findViewById(R.id.et_undertakeAmt2);
        etConfirmTermOrdId2 = (EditText)findViewById(R.id.et_confirmTermOrdId2);

        etMemberId3 = (EditText)findViewById(R.id.et_memberId3);
        etUndertakeAmt3 = (EditText)findViewById(R.id.et_undertakeAmt3);
        etConfirmTermOrdId3 = (EditText)findViewById(R.id.et_confirmTermOrdId3);

        radioGroup = findViewById(R.id.radioGroupID);
        btnAquire = findViewById(R.id.btn_acquire);
        btnScan = findViewById(R.id.btn_scan);
        view_line = findViewById(R.id.view_line);

        channelId = ChannelId.CHANNEL_BANK;


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == btnAquire.getId()) {
                    Timber.d( "onCheckedChanged: 选中银行卡");
                    channelId = ChannelId.CHANNEL_BANK;
                } else if (checkedId == btnScan.getId()) {
                    Timber.d( "onCheckedChanged: 选中扫码");
                    channelId = ChannelId.CHANNEL_SCAN;
                }
            }
        });

        updateOrdNo();
        btnComfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRefund();

            }
        });
    }
    private void doRefund(){
        String refundDetail = "";
        JSONArray refundDetailJsonArray;
        try {
            refundDetailJsonArray = new JSONArray();
            JSONObject jsonObject1 = new JSONObject();
            Timber.d("--etMemberId1.getText().toString()="+etMemberId1.getText().toString());
            jsonObject1.put("memberId",etMemberId1.getText().toString());         //扣款商户号
            jsonObject1.put("undertakeAmt",etUndertakeAmt1.getText().toString());     //承担金额
            jsonObject1.put("confirmTermOrdId",etConfirmTermOrdId1.getText().toString()); //原交易确认订单号
            Timber.d("--jsonObject1="+jsonObject1);
            if(!(isNullOrEmpty(etMemberId1.getText().toString())&&isNullOrEmpty(etUndertakeAmt1.getText().toString())&&isNullOrEmpty(etConfirmTermOrdId1.getText().toString()))){
                refundDetailJsonArray.put(jsonObject1);
            }

            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("memberId",etMemberId2.getText().toString());         //扣款商户号
            jsonObject2.put("undertakeAmt",etUndertakeAmt2.getText().toString());     //承担金额
            jsonObject2.put("confirmTermOrdId",etConfirmTermOrdId2.getText().toString()); //原交易确认订单号
            if(!(isNullOrEmpty(etMemberId2.getText().toString())&&isNullOrEmpty(etUndertakeAmt2.getText().toString())&&isNullOrEmpty(etConfirmTermOrdId2.getText().toString()))){
                refundDetailJsonArray.put(jsonObject2);
            }

            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("memberId",etMemberId3.getText().toString());         //扣款商户号
            jsonObject3.put("undertakeAmt",etUndertakeAmt3.getText().toString());     //承担金额
            jsonObject3.put("confirmTermOrdId",etConfirmTermOrdId3.getText().toString()); //原交易确认订单号
            if(!(isNullOrEmpty(etMemberId3.getText().toString())&&isNullOrEmpty(etUndertakeAmt3.getText().toString())&& isNullOrEmpty(etConfirmTermOrdId3.getText().toString()))){
                refundDetailJsonArray.put(jsonObject3);
            }
            Timber.d("--refundDetailJsonArray="+refundDetailJsonArray);
            if (refundDetailJsonArray.length() > 0){
                refundDetail = refundDetailJsonArray.toString();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(RefundRequestKey.KEY_CHANNEL_ID, channelId);
        jsonObject.addProperty(RefundRequestKey.KEY_MEMBER_ID, etMemberId.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_MER_OPER_ID, merOperId.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_MER_OPER_ID_PRINTED, merOperIdPrinted.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_ORD_AMT, etAmt.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_OORI_TERM_ORD_ID, etOriOrdNo.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_ORI_OUT_ORDER_ID, etOriOutOrdNo.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_OUT_ORD_ID, etOrdNo.getText().toString());
//        jsonObject.addProperty(RefundRequestKey.KEY_CASH_OUT_MODE, etCashOutMode.getText().toString());
        jsonObject.addProperty(RefundRequestKey.KEY_BG_RET_URL, "virgo://https://www.baidu.com/");

//        if (!isNullOrEmpty(refundDetail)){
//            jsonObject.addProperty(RefundRequestKey.KEY_REFUND_DETAIL, refundDetail);
//        }

        tvRequest.setText("接口:" + InterfaceType.Trans.IT_REFUND + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);

        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Trans.IT_REFUND);
        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);
                }
            }

        });

    }

    public static boolean isNullOrEmpty(String str){
        return str == null || "".equals(str.trim()) || "null".equals(str.trim());
    }

    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;
    }
}