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.manager.LoginActivity;
import com.chinapnr.pos.pay.demo.manager.ReprintActivity;
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;

public class ManagerActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvRequest, tvResult;
    private View view_line;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);

        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_mem_login).setOnClickListener(this);
        findViewById(R.id.btn_logout).setOnClickListener(this);
        findViewById(R.id.btn_get_msg).setOnClickListener(this);
        findViewById(R.id.btn_reprint).setOnClickListener(this);
        findViewById(R.id.btn_setting).setOnClickListener(this);

        tvRequest = findViewById(R.id.tv_request);
        tvResult = findViewById(R.id.tv_result);
        view_line = findViewById(R.id.view_line);

    }

    @Override
    public void onClick(View v) {
        tvRequest.setText("");
        tvResult.setText("");
        view_line.setVisibility(View.GONE);
        switch (v.getId()){
            case R.id.btn_login:
                doOperLogin();
                break;
            case R.id.btn_mem_login:
                doMemLogin();
                break;
            case R.id.btn_logout:
                doLogout();
                break;
            case R.id.btn_get_msg:
                doGetMsg();
                break;
            case R.id.btn_reprint:
                doReprint();
                break;
            case R.id.btn_setting:
                doSetting();
                break;
        }

    }

    private void doOperLogin(){
        Intent intent = new Intent(ManagerActivity.this, LoginActivity.class);
        intent.putExtra("TYPE", InterfaceType.Manager.IT_OPER_LOGIN);
        startActivity(intent);
    }
    private void doMemLogin(){
        Intent intent = new Intent(ManagerActivity.this, LoginActivity.class);
        intent.putExtra("TYPE", InterfaceType.Manager.IT_MEM_LOGIN);
        startActivity(intent);
    }

    private void doLogout() {
        view_line.setVisibility(View.VISIBLE);
        tvRequest.setText("接口：" + InterfaceType.Manager.IT_LOGOUT );
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Manager.IT_LOGOUT);
        PnrService.getInstance(getApplicationContext()).dealManage(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 doGetMsg() {
        view_line.setVisibility(View.VISIBLE);
        tvRequest.setText("接口：" + InterfaceType.Manager.IT_GET_MSG );
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Manager.IT_GET_MSG);
        PnrService.getInstance(getApplicationContext()).dealManage(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 doSetting() {
        view_line.setVisibility(View.VISIBLE);
        tvRequest.setText("接口：" + InterfaceType.Manager.IT_SETTING );
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Manager.IT_GET_MSG);
        PnrService.getInstance(getApplicationContext()).dealManage(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 doReprint() {
        Intent intent = new Intent(ManagerActivity.this, ReprintActivity.class);
        startActivity(intent);

    }
}