package com.example.beijing_daxing;

import androidx.databinding.DataBindingUtil;

import android.graphics.Bitmap;
import android.os.Bundle;
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.ActivityBfactivityBinding;
import com.example.beijing_daxing.dialog.TakePictureDialog;
import com.example.beijing_daxing.uitls.HexAscii;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.BFWm;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.uhf.api.cls.Reader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.pda.serialport.Tools;

public class BFActivity extends BaseActivity implements TakePictureDialog.dialogReturnListSizeCall {
    private BFWm wm;
    private TakePictureDialog takeDialog;
    private List<File> files;
    private List<Bitmap> bitmaps;

    private ActivityBfactivityBinding dataBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_bfactivity);
        wm = new BFWm();
        wm.setList(List.of(getResources().getStringArray(R.array.bfyy)));
        dataBinding.setVm(wm);
        takeDialog = new TakePictureDialog(this);
        takeDialog.setCall(this);
        bitmaps = new ArrayList<>();
        files = new ArrayList<>();
    }
    @Override
    protected void onResume() {
        super.onResume();
        setPower(SharedPreferencesUtils.getInt("powerScrapIn",17));
        uhfLongerManager.setGen2session(true);
        uhfLongerManager.asyncStartReading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uhfLongerManager.setGen2session(false);
        uhfLongerManager.asyncStopReading();
    }

    @Override
    public void sanRfid(List<Reader.TAGINFO> epcs) {
        var epc = epcs.get(0);
        byte[] epcId = epc.EpcId;
        String epc1 = HexAscii.hex2Str(Tools.Bytes2HexString(epcId, epcId.length));
        wm.setEpc(epc1);
        OkGo.<MyResult>post(url + "/bf/select")
                .tag(this)
                .params("epc", epc1)
                .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);
                            wm.setPcode(ledgerRfid.getBatchCode());
                            wm.setState(ledgerRfid.getDictLabel());
                        } else {
                            wm.clearText();
                            Toast.makeText(BFActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }

    public void bfTackPhoto(View view) {
        takePhoto(false,"");
    }

    public void bfShowPhoto(View view) {
        takeDialog.setBitmaps(bitmaps, files);
        takeDialog.show();
    }

    public void bf_submit4(View view) {
        bf_submit(4);
    }
    public void bf_submit(View view) {
        bf_submit(2);
    }
    private void bf_submit(int type) {

        String state = wm.getState();
        if (state==null ) return;
        if ( state.equals("正常")) {
            String reason = wm.getReason();
            String remark = wm.getRemark();
            if (reason.equals("其他") && remark ==null ) return;
            OkGo.<MyResult>post(url + "/bf/submit")
                    .params("epc", wm.getEpc())
                    .params("reason", reason)
                    .params("remark", remark)
                    .params("type", type)
                    .params("user", SharedPreferencesUtils.getstring("user", ""))
                    .addFileParams("files",files)
                    .execute(new MyRecultCall(dialog, this) {
                        @Override
                        public void onSuccess(Response<MyResult> response) {
                            super.onSuccess(response);
                            var body = response.body();
                            if (body.getCode() == 0) {
                                Toast.makeText(BFActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                files.forEach(File::delete);
                                finish();
                            } else {
                                Toast.makeText(BFActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

        } else {
            Toast.makeText(this, "异常轮挡，无需操作", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void returnListSize(int size) {
        dataBinding.bfPictureNumber.setText(bitmaps.size() + "");
    }

    @Override
    public void takePhotoCall(File outputImage, Bitmap bitmap) {
        files.add(outputImage);
        bitmaps.add(bitmap);
        dataBinding.bfPictureNumber.setText(bitmaps.size() + "");
    }
}