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

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
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.manager.MemLoginRequestKey;
import com.chinapnr.pos.config.key.manager.OperLoginRequestKey;
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;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_member, ll_user, ll_password;
    private EditText myName, myPassward, et_member;
    private TextView tvRequest, tvResult;
    private View view_line;
    private String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findViewById(R.id.btn_login).setOnClickListener(this);
        tvRequest = (TextView) findViewById(R.id.tv_request);
        tvResult = (TextView) findViewById(R.id.tv_result);
        myName = (EditText) findViewById(R.id.et_user);
        myPassward = (EditText) findViewById(R.id.et_pwd);
        et_member = (EditText) findViewById(R.id.et_member);

        ll_member = (LinearLayout) findViewById(R.id.ll_member);
        ll_user = (LinearLayout) findViewById(R.id.ll_user);
        ll_password = (LinearLayout) findViewById(R.id.ll_password);

        view_line = (View) findViewById(R.id.view_line);

        type = getIntent().getStringExtra("TYPE");

        if (InterfaceType.Manager.IT_OPER_LOGIN.equals(type)){
            setTitle("操作员登录");
            ll_member.setVisibility(View.GONE);
            ll_user.setVisibility(View.VISIBLE);
            ll_password.setVisibility(View.VISIBLE);
            myName.setText("testlc");
            myPassward.setText("F3dfedaa");
        }else if (InterfaceType.Manager.IT_MEM_LOGIN.equals(type)){
            setTitle("商户号登录");
            ll_member.setVisibility(View.VISIBLE);
            ll_user.setVisibility(View.GONE);
            ll_password.setVisibility(View.GONE);
            et_member.setText("6666000101737480");
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                if (InterfaceType.Manager.IT_OPER_LOGIN.equals(type)){
                    doOperLogin();
                }else if (InterfaceType.Manager.IT_MEM_LOGIN.equals(type)){
                    doMemLogin();
                }
                break;
        }
    }

    private void doOperLogin() {
        view_line.setVisibility(View.VISIBLE);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(OperLoginRequestKey.KEY_EMP_ACCOUNT, myName.getText().toString());
        jsonObject.addProperty(OperLoginRequestKey.KEY_EMP_PASS, myPassward.getText().toString());
        tvRequest.setText("接口：" + InterfaceType.Manager.IT_OPER_LOGIN + "\n" + "参数：" + jsonObject.toString());
        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Manager.IT_OPER_LOGIN);
        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 doMemLogin() {
        view_line.setVisibility(View.VISIBLE);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(MemLoginRequestKey.KEY_MEMBER_ID, et_member.getText().toString());
        tvRequest.setText("接口：" + InterfaceType.Manager.IT_MEM_LOGIN + "\n" + "参数：" + jsonObject.toString());

        jsonObject.addProperty(PnrRequestKey.KEY_INTERFACE_TYPE, InterfaceType.Manager.IT_MEM_LOGIN);
        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);
                }
            }
        });

    }
}