package com.ycgis.macall.personalcenter.v.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.callback.SimpleListClickListener;
import com.rs.macall.androidx.basemodel.utils.SmartRefreshLayoutHelp;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ActivityFrequentlyQuestionsBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.BaseBean;
import com.ycgis.macall.personalcenter.m.adapterbean.FrequentlyItemBean;
import com.ycgis.macall.personalcenter.m.requestbean.BaseRequestModel;
import com.ycgis.macall.personalcenter.m.requestbean.FrequentlyBean;
import com.ycgis.macall.personalcenter.p.callback.BaseRequestCallback;
import com.ycgis.macall.personalcenter.p.request.ApiModel;
import com.ycgis.macall.personalcenter.p.request.RetrofitService;
import com.ycgis.macall.personalcenter.v.adapter.FrequentlyQuestionsAdapter;
import com.ycgis.macall.personalcenter.v.fragment.FrequentlyQuestionsDetailsFragment;

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

/**
 * 常见问题
 */
public class FrequentlyQuestionsActivity extends BaseViewBindActivity<ActivityFrequentlyQuestionsBinding> {
    private FrequentlyQuestionsAdapter adapter;

    @Override
    protected ActivityFrequentlyQuestionsBinding getViewBinding() {
        return ActivityFrequentlyQuestionsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void init() {
        viewBinding.frequentlyRvList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        SmartRefreshLayoutHelp.setPullDownToRefresh(getContext(), viewBinding.frequentlySrlRefresh);
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void start() {
        viewBinding.baseTitle.leftImage.setImageDrawable(getDrawables(R.drawable.back_cliener));
        viewBinding.baseTitle.contentTitle.setText(R.string.frequentlyQuestions);
        viewBinding.baseTitle.titleleft.setOnClickListener(v -> finish());
        viewBinding.frequentlySrlRefresh.setOnRefreshListener(refreshLayout -> getFrequentlyData());
        viewBinding.frequentlySrlRefresh.autoRefresh();
    }

    private void getFrequentlyData() {
        ApiModel.request(RetrofitService.getBaseInstance().getQuestion(), new BaseRequestCallback<BaseRequestModel<List<FrequentlyBean>>>() {
            @Override
            public void onRequestFailure(String msg) {
                List<BaseBean> dataList = new ArrayList<>();
                BaseBean baseBean = new BaseBean() {
                };
                baseBean.setBaseType(-1);
                baseBean.setBaseMessage(msg);
                dataList.add(baseBean);
                initData(dataList);
            }

            @Override
            public void onRequestSuccess(BaseRequestModel<List<FrequentlyBean>> result) {
                int code = result.getCode();
                List<BaseBean> dataList = new ArrayList<>();
                if (code == 20041) {
                    List<FrequentlyBean> data = result.getData();
                    for (FrequentlyBean bean : data) {
                        List<FrequentlyItemBean> data1 = bean.getData();
                        bean.setBaseType(1);
                        dataList.add(bean);
                        for (int i = 0; i < data1.size(); i++) {
                            FrequentlyItemBean bean1 = data1.get(i);
                            bean1.setBaseType(2);
                            dataList.add(bean1);
                        }
                        dataList.get(dataList.size() - 1).setBaseType(3);
                    }
                    if (dataList.isEmpty()) {
                        BaseBean baseBean = new BaseBean() {
                        };
                        baseBean.setBaseType(0);
                        baseBean.setBaseMessage("暂无数据");
                        dataList.add(baseBean);
                    }
                } else {
                    BaseBean baseBean = new BaseBean() {
                    };
                    baseBean.setBaseType(-1);
                    baseBean.setBaseMessage(result.getMsg());
                    dataList.add(baseBean);
                }
                initData(dataList);
            }
        });
    }

    private void initData(List<BaseBean> baseBeans) {
        if (adapter == null) {
            adapter = new FrequentlyQuestionsAdapter(getContext(), baseBeans);
            viewBinding.frequentlyRvList.setAdapter(adapter);
            adapter.setListClickListener(new SimpleListClickListener() {
                @Override
                public void onSingleClick(View v, int pointer) {
                    BaseBean dataItem = adapter.getDataItem(pointer);
                    if (dataItem.getBaseType() == 2 || dataItem.getBaseType() == 3) {
                        FrequentlyItemBean bean = (FrequentlyItemBean) dataItem;
                        FrequentlyQuestionsDetailsFragment.newInstance(bean).show(getSupportFragmentManager(), "WorkbenchManageFragment");
                    }
                }
            });
        } else {
            adapter.upData(baseBeans);
        }
        viewBinding.frequentlySrlRefresh.finishRefresh();
    }


}