package com.chinapnr.pos.pay.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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.pay.demo.data.DayBillActivity;
import com.chinapnr.pos.pay.demo.data.QueryListActivity;
import com.chinapnr.pos.pay.demo.data.QueryOrderActivity;
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 DataActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tvRequest, tvResult;
    private View view_line;

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

        findViewById(R.id.btn_query_order).setOnClickListener(this);
        findViewById(R.id.btn_day_bill).setOnClickListener(this);
        findViewById(R.id.btn_query_list).setOnClickListener(this);
        findViewById(R.id.btn_balance_query).setOnClickListener(this);
        view_line = findViewById(R.id.view_line);
        tvRequest = findViewById(R.id.tv_request);
        tvResult = findViewById(R.id.tv_result);

    }

    @Override
    public void onClick(View v) {
        tvRequest.setText("");
        tvResult.setText("");
        view_line.setVisibility(View.GONE);
        switch (v.getId()){
            case R.id.btn_query_order:
                startActivity(new Intent(DataActivity.this, QueryOrderActivity.class));
                break;
            case R.id.btn_day_bill:
                startActivity(new Intent(DataActivity.this,DayBillActivity.class));
                break;
            case R.id.btn_query_list:
                startActivity(new Intent(DataActivity.this, QueryListActivity.class));
                break;
            case R.id.btn_balance_query:
                doQueryBalance();
                break;
        }

    }

    private void doQueryBalance(){
        tvRequest.setText("接口:" + InterfaceType.Data.IT_BALANCE_QUERY + "\n" + "参数：" + "");
        view_line.setVisibility(View.VISIBLE);

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Data.IT_BALANCE_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);
                }
            }

        });

    }
}