package com.zztrapp.zztrapp.ui.currency.activity;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.tencent.smtt.sdk.QbSdk;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.GetTreeListBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.databinding.ActivityFaLvFaGuiBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.currency.adapter.FaLvFaGuiAdapter;
import com.zztrapp.zztrapp.utilcode.klogs.KLog;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

import java.util.List;

/**
 * Describe 法律法规
 * Created by Bby on 2022/6/26
 * Email bby0856@163.com
 */
public class FaLvFaGuiActivity extends BaseActivity {


    private ActivityFaLvFaGuiBinding binding;
    private FaLvFaGuiAdapter adapter;
    private List<GetTreeListBean> list;
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private String countyCode;
    private String searchName = "";//搜索值
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityFaLvFaGuiBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText(R.string.laws_regulations);
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new FaLvFaGuiAdapter(this);
        binding.rv.setAdapter(adapter);

        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                // 内核初始化完成，可能为系统内核，也可能为系统内核
                KLog.e(" 内核初始化完成，可能为系统内核，也可能为系统内核");
            }

            /**
             * 预初始化结束
             * 由于X5内核体积较大，需要依赖网络动态下发，所以当内核不存在的时候，默认会回调false，此时将会使用系统内核代替
             * @param isX5 是否使用X5内核
             */
            @Override
            public void onViewInitFinished(boolean isX5) {
                KLog.e("isX5"+isX5);
            }
        });

        loadList();
    }


    @Override
    protected void initOnClick() {
        adapter.setOnItemClickListener(new FaLvFaGuiAdapter.OnItemClickListener() {
            @Override
            public void onOpenClick(int position, boolean isOpen) {
                list.get(position).setOpen(!isOpen);
                adapter.notifyItemChanged(position);
            }

            @Override
            public void onItemChildClick(String id) {
                Intent intent = new Intent(FaLvFaGuiActivity.this, FaLvFaGuiFileActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
    }

    //列表
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<List<GetTreeListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<GetTreeListBean>>>() {
            @Override
            public void onNext(HttpResult<List<GetTreeListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    list = httpResult.getResult();
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setOpen(false);
                    }
                    adapter.setData(list);
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<GetTreeListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, FaLvFaGuiActivity.this, isShowDialog);
        RxApiManager.get().add("getTreeList", httpResultProgressSubscriber);
        HttpMethods.getInstance(FaLvFaGuiActivity.this).getTreeList(httpResultProgressSubscriber);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("getTreeList");
    }
}