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

import android.os.Bundle;
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.data.QueryOrderRequestKey;
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.Map;

import timber.log.Timber;

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


    Button btnComfirm;
    TextView tvRequest, tvResult;
    EditText etMemberId, merOperId,merOperIdPrinted, etOrdNo,etOutOrdId;
    RadioGroup radioGroup, radioGroup_channel;
    RadioButton qrcodeU,qrcodeJ, btn_acquire, btn_scan;
    private View view_line;
    String payIsSale;
    String channelId;

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

        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);
        etOrdNo = (EditText) findViewById(R.id.et_ord_no);
        etOutOrdId = (EditText) findViewById(R.id.et_out_ord_no);
        radioGroup=(RadioGroup)findViewById(R.id.radioGroupID);
        qrcodeU=(RadioButton)findViewById(R.id.qrcode_u);
        qrcodeJ=(RadioButton)findViewById(R.id.qrcode_j);
        view_line=findViewById(R.id.view_line);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {
                if (checkedId==qrcodeU.getId()){
                    System.out.println("选中了正向交易!");
                    payIsSale = "1";
                }else if (checkedId==qrcodeJ.getId()){
                    System.out.println("选中了反向交易!");
                    payIsSale = "0";
                }
            } } );
        radioGroup_channel = findViewById(R.id.radioGroup);
        btn_acquire = findViewById(R.id.btn_acquire);
        btn_scan = findViewById(R.id.btn_scan);
        channelId = "acquire";
        radioGroup_channel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == btn_acquire.getId()) {
                    System.out.println("选中了银行卡!");
                    channelId = ChannelId.CHANNEL_BANK;
                } else if (checkedId == btn_scan.getId()) {
                    System.out.println("选中了扫码!");
                    channelId = ChannelId.CHANNEL_SCAN;
                }
            }
        });
        btnComfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doQueryOrder();
//                String uristr = "payment://com.newland.pospp/queryOrder?memberId=" + etMemberId.getText().toString()
//                        + "&merOperId=" + merOperId.getText().toString()
//                        + "&merOperIdPrinted=" + merOperIdPrinted.getText().toString()
//                        + "&merOrdId=" + etOrdNo.getText().toString()
//                        + "&selfOrdId=" + etOutOrdId.getText().toString()
//                        + "&isSale=" + payIsSale
//                        + "&channelId=" + channelId;
//                tvRequest.setText(uristr);
//                Uri uri = Uri.parse(uristr);
//                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
////                startActivity(intent);
//                startActivityForResult(intent, 1);
            }
        });
    }

    private void doQueryOrder(){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(QueryOrderRequestKey.KEY_MEMBER_ID, etMemberId.getText().toString());
        jsonObject.addProperty(QueryOrderRequestKey.KEY_MER_OPER_ID, merOperId.getText().toString());
        jsonObject.addProperty(QueryOrderRequestKey.KEY_MER_OPER_ID_PRINTED, merOperIdPrinted.getText().toString());
        jsonObject.addProperty(QueryOrderRequestKey.KEY_TERM_ORD_ID, etOrdNo.getText().toString());
        jsonObject.addProperty(QueryOrderRequestKey.KEY_OUT_ORD_ID, etOutOrdId.getText().toString());
        jsonObject.addProperty(QueryOrderRequestKey.KEY_IS_SALE, payIsSale);
        jsonObject.addProperty(QueryOrderRequestKey.KEY_CHANNEL_ID, channelId);
        tvRequest.setText("接口:" + InterfaceType.Data.IT_QUERY_ORDER + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Data.IT_QUERY_ORDER);
        PnrService.getInstance(getApplicationContext()).getData(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);
                }
            }

        });
    }
}