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.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.data.QueryListRequestKey;
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 QueryListActivity extends AppCompatActivity {

    Button btnComfirm;
    TextView tvRequest, tvResult;
    EditText etMemberId;
    private View view_line;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_list);

        btnComfirm = findViewById(R.id.btn_confirm);

        tvRequest = findViewById(R.id.tv_request);
        tvResult = findViewById(R.id.tv_result);
        etMemberId = findViewById(R.id.et_member_id);
        view_line = findViewById(R.id.view_line);
        btnComfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doQueryList();
            }
        });
    }

    private void doQueryList(){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(QueryListRequestKey.KEY_MEMBER_ID, etMemberId.getText().toString());
        tvRequest.setText("接口:" + InterfaceType.Data.IT_TRANS_QUERY + "\n" + "参数：" + jsonObject.toString());
        view_line.setVisibility(View.VISIBLE);
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Data.IT_TRANS_QUERY);
        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);
                }
            }

        });
    }
}