package xn.xxp.main.rule;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;

import java.util.ArrayList;
import java.util.List;

import core.ui.activity.BaseCountDownActivity;
import core.util.FastClickDelegate;
import http.client.ApiRepository;
import http.net.UrlValidator;
import http.vo.request.SafetyListReq;
import http.vo.response.SafeBook;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import xn.xxp.databinding.ActivityRuleBinding;
import xn.xxp.widget.ITitleBar;
import xn.xxp.widget.NavViewCompat;

public class RuleActivity extends BaseCountDownActivity<ActivityRuleBinding> {

    private ActivityRuleBinding binding;

    private String mSystemFlag = "1";
    private RuleAdapter ruleAdapter;
    private List<SafeBook> safeBookList = new ArrayList<>();

    @Override
    public ITitleBar getMTitleBar() {
        return binding.titleBar;
    }

    @Override
    public NavViewCompat getMNavView() {
        return binding.navView;
    }

    @Override
    protected ActivityRuleBinding createViewBinding() {
        return binding = ActivityRuleBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        // 学校制度
        binding.schoolSystem.setOnClickListener(new FastClickDelegate(new Function1<View, Unit>() {
            @Override
            public Unit invoke(View view) {
                mSystemFlag = "1";
                load();
                return null;
            }
        }));
        // 学院制度
        binding.collegeSystem.setOnClickListener(new FastClickDelegate(new Function1<View, Unit>() {
            @Override
            public Unit invoke(View view) {
                mSystemFlag = "2";
                load();
                return null;
            }
        }));
        // 中心制度
        binding.centerSystem.setOnClickListener(new FastClickDelegate(new Function1<View, Unit>() {
            @Override
            public Unit invoke(View view) {
                mSystemFlag = "3";
                load();
                return null;
            }
        }));

        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryData();
            }
        });
        ruleAdapter = new RuleAdapter(safeBookList);
        binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showLoading("请稍候...");
                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<SafeBook>() {
                    @Override
                    public SafeBook doInBackground() throws Throwable {
                        SafeBook safeBook = ruleAdapter.getItem(position);
                        if (TextUtils.isEmpty(safeBook.content)) {
                            showToast("数据异常,无法展示,请重新上传!");
                            return null;
                        }
                        if (!UrlValidator.isDownloadable(safeBook.content)) {
                            return null;
                        }

                        return safeBook;
                    }

                    @Override
                    public void onSuccess(SafeBook result) {
                        dismissLoading();
                        Intent intent = new Intent(RuleActivity.this, RuleDetailActivity.class);
                        intent.putExtra("data", result);
                        intent.putExtra("onEboard", true);
                        startActivity(intent);
                    }
                });
            }
        });

        binding.listView.setAdapter(ruleAdapter);

        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryData();
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        load();
    }

    private void load() {
        binding.swipeRefreshLayout.setRefreshing(true);
        queryData();
    }

    private Disposable disposable;

    private void queryData() {
        if (null != disposable) {
            removeDisposable(disposable);
        }
        disposable = ApiRepository.INSTANCE.safeBookList(mSystemFlag).subscribe(new Consumer<List<SafeBook>>() {
            @Override
            public void accept(List<SafeBook> safeBooks) throws Throwable {
                dispatchLoadDataSuccess(safeBooks);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Throwable {
                LogUtils.e(Log.getStackTraceString(throwable));
                dispatchLoadDataFailure(throwable);
            }
        });
        addDisposable(disposable);
    }

    private void dispatchLoadDataFailure(Throwable throwable) {
        LogUtils.e(Log.getStackTraceString(throwable));
        if (!isDestroyed()) {
            binding.swipeRefreshLayout.setRefreshing(false);
            showNetError(throwable);
        }
    }

    private void dispatchLoadDataSuccess(List<SafeBook> safeBooks) {
        if (!isDestroyed()) {
            binding.swipeRefreshLayout.setRefreshing(false);
            if (null != safeBooks && !safeBooks.isEmpty()) {
                safeBookList.clear();
                safeBookList.addAll(safeBooks);
                ruleAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onDestroy() {
        binding.swipeRefreshLayout.setRefreshing(false);
        super.onDestroy();
    }
}