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

public class FlowerChokActivity extends AppCompatActivity {

    Button btnComfirm;
    TextView tvRequest, tvResult;
    EditText etMemberId,  merOperId,merOperIdPrinted, etOrdNo  ,etAmt;
    EditText etPayChannel,etPayKind,etFlowerChokingNum,etHbFqSellerPercent,etGoodsDesc,etBgUrl,etMerpirv,etRemark;
    private View view_line;

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

        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);
        etPayChannel = (EditText) findViewById(R.id.et_pay_channel);
        etOrdNo = (EditText) findViewById(R.id.et_ord_no);
        etAmt = (EditText) findViewById(R.id.et_amount);

        etPayKind = (EditText) findViewById(R.id.et_pay_kind);
        etFlowerChokingNum = (EditText) findViewById(R.id.et_flower_choking_num);
        etHbFqSellerPercent = (EditText) findViewById(R.id.et_hbFqSellerPercent);
        etGoodsDesc = (EditText) findViewById(R.id.et_goods_desc);
        etBgUrl = (EditText) findViewById(R.id.et_bg_url);
        etMerpirv = (EditText) findViewById(R.id.et_merpirv);
        etRemark = (EditText) findViewById(R.id.et_remark);
        view_line = findViewById(R.id.view_line);

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

    }

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

        jsonObject.addProperty(HBFQRequestKey.KEY_CHANNEL_ID, etPayChannel.getText().toString());
//        jsonObject.addProperty(HBFQRequestKey.KEY_MOBILE_PAY_TYPE, etPayKind.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_ORD_AMT, etAmt.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_HBFQ_NUM, etFlowerChokingNum.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_OUT_ORD_ID, etOrdNo.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_HBFQ_SELLER_PERCENT, etHbFqSellerPercent.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_GOODS_DESC, etGoodsDesc.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_MER_PRIV, etMerpirv.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_GOODS_DESC, etGoodsDesc.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_ORD_REMARK, etRemark.getText().toString());
        jsonObject.addProperty(HBFQRequestKey.KEY_BG_RET_URL, etBgUrl.getText().toString());
        tvRequest.setText("接口:" + InterfaceType.Trans.IT_HBFQ + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Trans.IT_HBFQ);
        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;
    }
}