package com.wmdigit.wmposdemo.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.wmdigit.wmpos.WmAceKG;
import com.wmdigit.wmpos.bean.ScaleSetting;
import com.wmdigit.wmpos.http.OnBindPosListener;
import com.wmdigit.wmpos.http.OnQueryPosRegisterInfoListener;
import com.wmdigit.wmpos.utils.LogUtils;
import com.wmdigit.wmpos.utils.SPStaticUtils;
import com.wmdigit.wmposdemo.R;
import com.wmdigit.wmposdemo.databinding.ActivityRegisterBinding;
import com.wmdigit.wmposdemo.utils.Constant;


public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityRegisterBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        binding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WmAceKG.queryPosByMac(new OnQueryPosRegisterInfoListener() {
                    @Override
                    public void onSuccess(String posId, String snCode, String tenant) {
                        Toast.makeText(RegisterActivity.this, "获取注册信息成功,已为您自动填写", Toast.LENGTH_SHORT).show();
                        binding.posEdit.setText(posId);
                        binding.posSnCode.setText(snCode);
                        binding.accountCompany.setText(tenant);
                    }

                    @Override
                    public void onFailure(int code, String message) {
                        Toast.makeText(RegisterActivity.this, "获取失败,返回信息:\n"+message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        binding.tvSign.setOnClickListener(v -> {
            String pos = binding.posEdit.getText().toString();
            String companyCode = binding.accountCompany.getText().toString();
            String snCode = binding.posSnCode.getText().toString().trim();
            if (TextUtils.isEmpty(pos)) {
                return;
            }
            if (TextUtils.isEmpty(companyCode)) {
                return;
            }
            if (TextUtils.isEmpty(snCode)) {
                return;
            }
            WmAceKG.initPos(pos, companyCode, snCode, new OnBindPosListener() {
                @Override
                public void bindFail(int code, String message) {
                    LogUtils.e(message);
                    Toast.makeText(RegisterActivity.this, "绑定失败,返回信息:\n"+message, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void bindLoading() {
                    Toast.makeText(RegisterActivity.this, "审核中,请联系工作人员", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void bindSuccess() {
                    SPStaticUtils.put(Constant.ACCOUNT, companyCode);
                    SPStaticUtils.put(Constant.POS_CODE, pos);
                    SPStaticUtils.put(Constant.SN_CODE, snCode);
                    SPStaticUtils.put(Constant.BIND, true);
                    boolean isScale = WmAceKG.getScaleSetting();
                    if (!isScale) {
                        Toast.makeText(RegisterActivity.this, "您未裁剪秤盘,为您跳转至裁剪页", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, CropPicActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Intent intent1 = new Intent(RegisterActivity.this, WeighingActivity.class);
                        startActivity(intent1);
                    }
                }
            });
        });
    }
}