package com.example.writeepc;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.example.writeepc.base.BaseActivity;
import com.example.writeepc.base.okgo.MyRecultCall;
import com.example.writeepc.base.okgo.MyResult;
import com.example.writeepc.broadcast.MyScannerCall;
import com.example.writeepc.broadcast.MyScannerReceiver;
import com.example.writeepc.databinding.ActivityShuangqianBinding;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

public class ShuangqianActivity extends BaseActivity implements MyScannerCall {

    private ActivityShuangqianBinding binding;
    private MyScannerReceiver myReceiver;
    private String uriLastStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_shuangqian);
        initUHFRManager1();
        initDialog();

        myReceiver = new MyScannerReceiver(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.rfid.SCAN");
        registerReceiver(myReceiver, intentFilter);
        boolean type = getIntent().getBooleanExtra("type", true);
        binding.setTitle(type ? "MES轮胎绑定" : "解绑");
        binding.setType(type);
        uriLastStr = type ? "barcodeAndRFIDBinding" : "unBinding";
    }

    // 读取
    public void readRFID(View view) {
        readEPC(12);
    }

    @Override
    protected void readTidReault(boolean readState, String result, byte[] rdata) {
        super.readTidReault(readState, result, rdata);
        binding.socketEpc.setText(result);
    }

    // 清空数据
    public void clearView(View view) {
        binding.socketBarcode.setText(null);
        binding.socketEpc.setText(null);
    }

    // 提交数据
    public void submit(View view) {
        String epc = binding.socketEpc.getText().toString();
        String code = binding.socketBarcode.getText().toString();
        if (epc.isEmpty() || code.isEmpty()) return;
        haohuaSubmit(epc, code);
        // callWebService(buildSoapRequest(epc, code));//双钱
    }

    //   解绑
    public void unBindingSubmit(View view) {
        String epc = binding.socketEpc.getText().toString();
        String code = binding.socketBarcode.getText().toString();

        haohuaSubmit(epc, code);
    }

    private void haohuaSubmit(String epc, String code) {
        OkGo.<MyResult>post("http://192.168.26.26:5050/UMInterface/RFID/" + uriLastStr)
                .params("rfid", epc).params("barCode", code).execute(new MyRecultCall(dialog, this) {
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        MyResult body = response.body();
                        try {
                            if (body.getCode() == 200) {
                                tipsDialog.setTip(true, body.getInfo());
                                tipsDialog.show();
                                binding.socketBarcode.setText(null);
                                binding.socketEpc.setText(null);
                            } else {
                                tipsDialog.setTip(false, body.getInfo());
                                tipsDialog.show();
                            }
                        } catch (Exception e) {
                            tipsDialog.setTip(false, "接口返回信息异常");
                            tipsDialog.show();
                        }

                    }
                });

    }


    private void callWebService(String soapRequest) {
        // 发送 POST 请求
        OkGo.<String>post("http://10.135.55.206:99/WebService.asmx").headers("Content-Type", "text/xml; charset=utf-8").upString(soapRequest, mediaType).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                dialog.show();
            }

            @Override
            public void onSuccess(Response<String> response) {
                // 处理响应结果
                String result = response.body();
                try {
                    String str = "<faultstring>";
                    int faultstringIndex = result.indexOf(str);
                    if (faultstringIndex > -1) {
                        String faultstring = result.substring(faultstringIndex + str.length(), result.indexOf("</faultstring>"));
                        tipsDialog.setTip(false, faultstring);
                        tipsDialog.show();
                        return;
                    }
                    int codeIndex = result.indexOf("<Code>");
                    if (codeIndex > -1) {
                        String resultCode = result.substring(codeIndex + 6, result.indexOf("</Code>"));
                        String resultMsg = result.substring(result.indexOf("<Msg>") + 5, result.indexOf("</Msg>"));
                        if (resultCode.equals("200")) {
                            tipsDialog.setTip(true, resultMsg);
                            tipsDialog.show();
                            binding.socketBarcode.setText(null);
                            binding.socketEpc.setText(null);
                        } else {
                            tipsDialog.setTip(false, resultMsg);
                            tipsDialog.show();
                        }

                        // clearView(null);
                    }
                } catch (Exception e) {
                    tipsDialog.setTip(false, "接口返回信息异常");
                    tipsDialog.show();
                }
            }


            @Override
            public void onFinish() {
                super.onFinish();
                dialog.dismiss();
            }
        });
    }

    // 创建提交信息
    private String buildSoapRequest(String epcid, String barcode) {
        String soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<soap:Body>" +
                "<RFID_BarcodBind xmlns=\"http://tempuri.org/\">" +
                "<epcid>" + epcid + "</epcid>" +
                "<barcode>" + barcode + "</barcode>" +
                "</RFID_BarcodBind>" +
                "</soap:Body>" +
                "</soap:Envelope>";
        return soapRequest;
    }


    @Override
    public void getScannerData(String data) {
        binding.socketBarcode.setText(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }
}