package com.example.iot_controlhost.ui.dialog;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.adapter.LogAdapter;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogLogBinding;
import com.example.iot_controlhost.databinding.ItemLogBinding;
import com.example.iot_controlhost.model.Log;
import com.example.iot_controlhost.ui.view.CustomSpinnerAdapter;
import com.example.iot_controlhost.utils.DialogUtil;
import com.example.iot_controlhost.utils.database.LogDBManager;
import com.example.iot_controlhost.utils.log.MyLog;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxAsyncTask;

import java.util.List;

/**
 * 体质弹窗
 */
public class LogDialog extends BaseDialog<DialogLogBinding> {

    /**
     * 当前主界面日志所在页数
     */
    private int logPage = 1;
    List<Log> logs;
    LogAdapter adapter;

    public LogDialog(Context mContext, List<Log> logs) {
        super(mContext);
        this.logs = logs;
        adapter = new LogAdapter(logs, ItemLogBinding.class);
        //初始化界面控件
        binding.rvLog.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvLog.setAdapter(adapter);
        binding.acInput.setHint(mContext.getString(R.string.select_log_tag));
        binding.acInput.setAdapter(new CustomSpinnerAdapter(mContext, MyLog.getUserQueryTag().toArray(new String[0])));
        binding.refreshLayout.setOnRefreshListener(refreshlayout -> RxJavaUtils.executeAsyncTask(new RxAsyncTask<Object, List<Log>>(null) {
            @Override
            public List<Log> doInIOThread(Object o) {
                logPage = 1;
                return LogDBManager.queryList(binding.acInput.getText().toString(), logPage);
            }

            @Override
            public void doInUIThread(List<Log> logs) {
                adapter.setDataList(logs);
                if (logs.size() == 0) {
                    refreshlayout.finishLoadMoreWithNoMoreData();
                } else {
                    refreshlayout.finishLoadMore();
                }
                refreshlayout.finishRefresh();
            }
        }));
        binding.refreshLayout.setOnLoadMoreListener(refreshlayout -> RxJavaUtils.executeAsyncTask(new RxAsyncTask<Object, List<Log>>(null) {
            @Override
            public List<Log> doInIOThread(Object o) {
                return LogDBManager.queryList(binding.acInput.getText().toString(), ++logPage);
            }

            @Override
            public void doInUIThread(List<Log> logs) {
                adapter.addDataList(logs);
                if (logs.size() == 0) {
                    refreshlayout.finishLoadMoreWithNoMoreData();
                } else {
                    refreshlayout.finishLoadMore();
                }
            }
        }));
        binding.btnQuery.setOnClickListener(view -> {
            logPage = 1;
            DialogUtil.showLoadingDialog(mContext, "正在读取日志...");
            RxJavaUtils.executeAsyncTask(new RxAsyncTask<Object, List<Log>>(null) {
                @Override
                public List<Log> doInIOThread(Object o) {
                    return LogDBManager.queryList(binding.acInput.getText().toString(), logPage);
                }

                @Override
                public void doInUIThread(List<Log> logs) {
                    adapter.setDataList(logs);
                    DialogUtil.hideLoadingDialog();
                }
            });
        });
    }

}
