package com.naz.sdkdemo.activity;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.bean.TrademarkCheckInfo;
import com.naz.sdkdemo.helper.SerialIPManager;
import com.naz.sdkdemo.http.Api;
import com.naz.sdkdemo.okgo.callback.DialogCallback;
import com.naz.sdkdemo.okgo.module.BaseResponse2;
import com.naz.sdkdemo.weight.UserCache;

import org.json.JSONException;
import org.json.JSONObject;

public class VerifyTrademarkActivity extends BaseActivity {
    private EditText etSearch;
    private TextView tvSearch;
    private SerialIPManager serialIPManager;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_trademark;
    }

    @Override
    protected void initView() {
        etSearch = findViewById(R.id.et_search);
        tvSearch = findViewById(R.id.tv_search);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etSearch.getText().toString().equals("")){
                    Toast.makeText(VerifyTrademarkActivity.this, "请扫描派工单号或输入销售订单号", Toast.LENGTH_SHORT).show();
                    return;
                }
                getInfo(etSearch.getText().toString());
            }
        });
        getCodeInfo();
    }

    private void getCodeInfo() {
        serialIPManager = new SerialIPManager();
        serialIPManager.init();
        serialIPManager.connect(UserCache.getIP(), UserCache.getPort()+12, new SerialIPManager.ConnectCallBack() {
            @Override
            public void connectResult(int code) {
                if (code == 0){
                    Toast.makeText(VerifyTrademarkActivity.this, "请确认串口服务器正常开启", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void receiveData(String data) {
                String[] a = data.split("code=");
                if (a.length > 0){
                    String b = a[a.length-1];
                    String[] c= b.split("&");
                    if (c.length > 0){
                        data = c[0];
                    }
                }
                etSearch.setText(data);
                getInfo(etSearch.getText().toString());
            }
        });
    }

    private void getInfo(String codeStr) {
        JSONObject body = new JSONObject();
        try {
            body.put("brand_qr_code", codeStr);
        } catch (JSONException e) {
        }
        OkGo.<BaseResponse2<TrademarkCheckInfo>>post(Api.checkBrandUrl())
                .tag(this)
                .upJson(body)
                .execute(new DialogCallback<BaseResponse2<TrademarkCheckInfo>>(this) {
                    @Override
                    public void onSuccess(Response<BaseResponse2<TrademarkCheckInfo>> response) {
                        updateView(response.body().data);
                    }
                });
    }

    private void updateView(TrademarkCheckInfo info){
        TextView tvOrderId = findViewById(R.id.tv_order_id);
        TextView tvDealer = findViewById(R.id.tv_dealer);
        TextView tvSupplier = findViewById(R.id.tv_supplier_plate);
        TextView tvSupplierPaper = findViewById(R.id.tv_supplier_paper);
        TextView tvDate = findViewById(R.id.tv_verify_date);
        tvOrderId.setText(info.order_no);
        tvDealer.setText(info.jingxiaoshang);
        tvSupplier.setText(info.supplier_name);
        tvSupplierPaper.setText(info.paper_mill);
        tvDate.setText(info.check_date);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        serialIPManager.close(new SerialIPManager.CloseCallBack() {
            @Override
            public void closeResult(int code) {

            }
        });
    }
}
