package com.example.beijing_daxing;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

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.been.LedgerRfid;
import com.example.beijing_daxing.databinding.ActivityFpInBinding;
import com.example.beijing_daxing.uitls.HexAscii;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.FpInVm;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;


public class FpInActivity extends BaseActivity {
    private FpInVm vm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityFpInBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_fp_in);
        vm = new FpInVm();
        binding.setVm(vm);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setPower(SharedPreferencesUtils.getInt("powerScrapIn",17));
        uhfrManager.startInventoryTag();
    }

    @Override
    public void sanRfid(String epc) {
        vm.setEpc(HexAscii.hex2Str(epc));
        OkGo.<MyResult>post(url+"/fpin/select")
                .tag(this).params("epc",epc)
                .execute(new MyRecultCall(dialog,this){
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getCode()==0){
                            LedgerRfid ledgerRfid=gson.fromJson(body.getData().toString(),LedgerRfid.class);
                            vm.setBichCode(ledgerRfid.getBatchCode());
                            vm.setLocation(ledgerRfid.getLocationType());
                            vm.setLocationCode(ledgerRfid.getLocationCode());
                            vm.setState(ledgerRfid.getDictLabel());
                            vm.setTime(ledgerRfid.getCreateTime());
                            vm.setList(ledgerRfid.getBaseLocationList());
                        }
                        Toast.makeText(FpInActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void fpin_submit(View view){
        // Log.e("TAG", "onSuccess:" +vm.toString() );
        var state = vm.getState();
        if (!state.equals("报废")){
            Toast.makeText(this, "轮挡状态："+state+",不能使用废品入库功能", Toast.LENGTH_SHORT).show();
            return;
        }
        if (vm.getLocation().equals("仓库")){
            Toast.makeText(this, "轮挡状态已在仓库", Toast.LENGTH_SHORT).show();
            return;
        }
        OkGo.<MyResult>post(url+"/fpin/submit").tag(this)
                .params("epc",vm.getEpc())
                .params("localtionCode",vm.getLocationCode())
                .params("user", SharedPreferencesUtils.getstring("user",""))
                .params("bf",vm.getTime())
                .params("subCode",vm.getSubmitCode())
                .execute(new MyRecultCall(dialog,this){
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        var body = response.body();
                        if (body.getCode()==0){
                            finish();
                            Toast.makeText(FpInActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Toast.makeText(FpInActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });


    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("TAG", "onKeyDown:" + keyCode);
        if (keyCode == 191) {

            handler.postDelayed(runnable, 10);

        }
        return super.onKeyDown(keyCode, event);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Log.e("TAG", "run:" );
            sanEpc();
            handler.removeCallbacks(runnable);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        handler = null;
        runnable = null;
        uhfrManager.stopInventory();
    }
}