package com.example.beijing_daxing;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;

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

import com.example.beijing_daxing.adapter.CheckListAdapter;
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.been.CheckTaskInfo;
import com.example.beijing_daxing.been.Stock;
import com.example.beijing_daxing.databinding.ActivityCheckListBinding;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
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.List;

public class CheckListActivity extends BaseActivity implements AdapterClickCall {
    private CheckListAdapter adapter;
    private List<CheckTaskInfo> list;
    private ObservableBoolean createState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCheckListBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_check_list);
        adapter = new CheckListAdapter(this,this);

        binding.setAdapter(adapter);
        createState=new ObservableBoolean();
        createState.set(false);
        binding.setState(createState);
        initRequest();
    }

    private void initRequest() {
        OkGo.<MyResult>post(url+"/inspection/selectTask")
                .params("user", SharedPreferencesUtils.getstring("user", ""))
                .execute(new MyRecultCall(dialog, this) {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void onSuccess(Response<MyResult> response) {
                        super.onSuccess(response);
                        MyResult body = response.body();
                        if (body.getCode()==0){
                            createState.set(false);
                            list=gson.fromJson(body.getData().toString(),new TypeToken<List<CheckTaskInfo>>(){}.getType());
                            adapter.setList(list);
                            adapter.notifyDataSetChanged();
                        }else {
                            createState.set(true);
                        }

                    }
                });
    }

    @Override
    protected void sanRfid(List<Reader.TAGINFO> epcs) {

    }

    public void check_start(View view) {
        OkGo.<MyResult>post(url + "/xj/createTask")
                .params("user", SharedPreferencesUtils.getstring("user", ""))
                .execute(new MyRecultCall(dialog, this) {
            @Override
            public void onSuccess(Response<MyResult> response) {
                super.onSuccess(response);
                var body = response.body();
                if (body.getCode() == 0) {
                    initRequest();
                    return;
                }
                Toast.makeText(CheckListActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void clickItem(int index) {
        Intent intent=new Intent(this,CheckActivity.class);
        intent.putExtra("id",list.get(index).getId());
        startActivity(intent);
    }
}