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.TextView;

import androidx.appcompat.app.AppCompatActivity;

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.AuthSaleVoidRequestKey;
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.Locale;
import java.util.Map;
import java.util.Random;

import timber.log.Timber;

/**
 * @author shuaifeng.zhao
 */
public class AuthSaleVoidActivity extends AppCompatActivity {

    TextView tvRequest, tvResult;
    EditText etMemberId, merOperId,merOperIdPrinted, etOriVoucher, etOrdNo;
    Button btnAcquire;
    private View view_line;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth_sale_void);
        btnAcquire = (Button) findViewById(R.id.btn_acquire);

        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);
        view_line = findViewById(R.id.view_line);

        updateOrdNo();

        btnAcquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doAuthSaleVoid();
            }
        });
    }

    private void doAuthSaleVoid(){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_MEMBER_ID, etMemberId.getText().toString());
        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_MER_OPER_ID, merOperId.getText().toString());
        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_MER_OPER_ID_PRINTED, merOperIdPrinted.getText().toString());


        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_ORI_VOUCHER_NO, etOriVoucher.getText().toString());
        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_OUT_ORD_ID, etOrdNo.getText().toString());
        jsonObject.addProperty(AuthSaleVoidRequestKey.KEY_BG_RET_URL, "https://www.baidu.com/");
        tvRequest.setText("接口:" + InterfaceType.Trans.IT_AUTH_SALE_VOID + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);

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