package com.example.jinyu_rfid;

import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.jinyu_rfid.base.BaseActivity;
import com.example.jinyu_rfid.base.MyRecultCall;
import com.example.jinyu_rfid.base.MyResult;
import com.example.jinyu_rfid.been.ReadTyreNoResult;
import com.example.jinyu_rfid.broadcast.ScanERCodeReceiver;
import com.example.jinyu_rfid.callback.DataReturnCall;
import com.example.jinyu_rfid.databinding.ActivityWriteUserBinding;
import com.example.jinyu_rfid.rfid.C5106Device;
import com.example.jinyu_rfid.rfid.RFIDModel;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;

public class WriteUserActivity extends BaseActivity implements DataReturnCall {
    private Intent intent1;
    private Intent intent2;
    private RFIDModel rfidModel;
    private ActivityWriteUserBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_write_user);
        binding.setTitle(getString(R.string.activity_name_configuration));
        rfidModel = new C5106Device();
        intent1 = new Intent(this, ConfigurationTableActivity.class);
        intent2 = new Intent(this, ConfigurationTableActivity.class);
        ScanERCodeReceiver scanERCodeReceiver = new ScanERCodeReceiver(this);
        registerReceiver(scanERCodeReceiver, new IntentFilter("com.rfid.SCAN"));


    }


    public void writeReadEPC(View view) {

    }

    public void writeUser(View view) {

    }


    public void writeGoRead(View view) {
        startActivity(intent1);
    }

    public void writeGoConfiguration(View view) {
        startActivity(intent2);
    }

    @Override
    public void readEpcCodeInfo(String epc) {
        binding.writeEpcCode.setText(epc);
    }

    @Override
    public void readUserInfo(String user) {

    }

    @Override
    public void readerCodeInfo(String erCode) {
        binding.writeTire.setText(erCode);
        Map<String,String> map=new HashMap<>();
        map.put("TyreNo", erCode);
        map.put("token","123456");
        OkGo.<MyResult>post(url + "/readTyreNo")
                .upRequestBody(RequestBody.create(JSON,gson.toJson(map)))
                .execute(new MyRecultCall(dialog, this) {
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getResultFlag().equals("1")){
                            List<ReadTyreNoResult> list=gson.fromJson(body.getJson(),new TypeToken<List<ReadTyreNoResult>>(){}.getType());
                            return;
                        }
                        Toast.makeText(WriteUserActivity.this, body.getResultData().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}