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.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
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.dialog.InPutDialog;
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 CheckListAdapter.AdapterClickCall, InPutDialog.InPutDialogCall {
    private CheckListAdapter adapter;
    private List<CheckTaskInfo> list;
    private ObservableBoolean createState;
    private String[] stateList;
    private InPutDialog inputDialog;

    @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);
        inputDialog = new InPutDialog(this);
        inputDialog.setInPutDialogCall(this);
        stateList = getResources().getStringArray(R.array.checkState);
        binding.checkListState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String state = stateList[position];
                Log.e("TAG", "onItemSelected:" + state);
                initRequest(state);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initRequest(String state) {
        OkGo.<MyResult>post(url + "/inspection/selectTask")
                .params("state", state)
                .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);
                        }

                    }
                });
    }


    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);
        CheckTaskInfo checkTaskInfo = list.get(index);
        intent.putExtra("id", checkTaskInfo.getId());
        intent.putExtra("inspectionId", checkTaskInfo.getInspectionId());
        intent.putExtra("code", checkTaskInfo.getLocationCode());
        intent.putExtra("backIndex", index);
        startActivityForResult(intent, 1);
    }
private int skipIndex;
    @Override
    public void clickItemSkip(int index) {
        skipIndex=index;
        inputDialog.show();
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && data != null) {
            int index = data.getIntExtra("backIndex", 0);
            Log.e("TAG", "onActivityResult:" + index);
            list.get(index).setTaskState("已完成");
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void inPutDialogReculi(String name) {
        CheckTaskInfo checkTaskInfo = list.get(skipIndex);
        OkGo.<MyResult>post(url + "/inspection/skipTask")
                .params("remark", name)
                .params("tableId", checkTaskInfo.getId())
                .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) {
                            checkTaskInfo.setTaskState("跳过");
                            adapter.notifyDataSetChanged();
                        }

                    }
                });
        inputDialog.dismiss();
    }
}