package com.example.beijing_daxing;

import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.beijing_daxing.adapter.InAdapter;
import com.example.beijing_daxing.base.AdapterClickCall;
import com.example.beijing_daxing.base.BaseActivity;
import com.example.beijing_daxing.base.MyRecultCall;
import com.example.beijing_daxing.base.MyResult;
import com.example.beijing_daxing.databinding.ActivityOutBinding;
import com.example.beijing_daxing.been.BaseLocation;
import com.example.beijing_daxing.uitls.HexAscii;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.OutStoreVm;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.uhf.api.cls.Reader;

import java.util.ArrayList;
import java.util.List;

import cn.pda.serialport.Tools;
import okhttp3.RequestBody;

public class OutActivity extends BaseActivity implements AdapterClickCall {
    private InAdapter adapter;
    private OutStoreVm outVm;
    private List<String> epcList;
    private List<String> tagList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityOutBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_out);
        adapter=new InAdapter(this);
        epcList=new ArrayList<>();
        tagList=new ArrayList<>();
        adapter.setList(epcList);
        adapter.setAdapterClickCall(this);
        binding.setAdapter(adapter);
        outVm=new OutStoreVm();
        binding.setVm(outVm);
        initRequest();

    }
    @Override
    protected void onResume() {
        super.onResume();
        setPower(SharedPreferencesUtils.getInt("powerOut",17));
    }
    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void sanRfid(List<Reader.TAGINFO> epcs) {
        epcs.forEach(t -> {
            var epc = Tools.Bytes2HexString(t.EpcId,t.EpcId.length);
            if (!epc.startsWith("53574C44")) return;
            if (tagList.contains(epc)) return;
            tagList.add(epc);
            epcList.add(HexAscii.hex2Str(epc));
        });
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void clickItem(int index) {
        tagList.remove(index);
        epcList.remove(index);
        adapter.notifyDataSetChanged();
    }
    private void initRequest() {
        OkGo.<MyResult>post(url + "/outStore/select").tag(this).execute(new MyRecultCall(dialog, this) {
            @Override
            public void onSuccess(Response<MyResult> response) {
                super.onSuccess(response);
                 List<BaseLocation> baseLocationList=gson.fromJson(response.body().getData().toString(), new TypeToken<List<BaseLocation>>(){}.getType());
                outVm.setAreaList(baseLocationList);
            }
        });
    }
    public void out_submit(View view) {
        String useUser = outVm.getUseUser();
        if (useUser == null || epcList.isEmpty()) return;
        outVm.setEpcList(epcList);
        outVm.setSubmitUser(SharedPreferencesUtils.getstring("user","test"));
        OkGo.<MyResult>post(url + "/outStore/submit")
                .upRequestBody(RequestBody.create(JSON, gson.toJson(outVm)))
                .execute(new MyRecultCall(dialog,this){
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        if (response.body().getCode()==0){
                            Toast.makeText(OutActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else {
                            Toast.makeText(OutActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}