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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

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

import com.google.gson.JsonObject;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.callback.SimpleListClickListener;
import com.rs.macall.androidx.basemodel.utils.LogUtils;
import com.rs.macall.androidx.basemodel.utils.SmartRefreshLayoutHelp;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ActivityMySuggestionsListBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.FeedbackBean;
import com.ycgis.macall.personalcenter.m.requestbean.BaseRequestModel;
import com.ycgis.macall.personalcenter.m.requestbean.PagingModel;
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.FeedBackListAdapter;

import org.jetbrains.annotations.NotNull;

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

/**
 * 我的意见建议
 */
public class MySuggestionsListActivity extends BaseViewBindActivity<ActivityMySuggestionsListBinding> {
    private int total, page, pageSize = 20;
    private FeedBackListAdapter adapter;

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

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

    @Override
    protected void init() {
        SmartRefreshLayoutHelp.setRefreshAndLoad(getContext(), viewBinding.suggestionsRslRefresh);
        viewBinding.suggestionsRvList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.suggestionsRslRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                viewBinding.suggestionsRslRefresh.resetNoMoreData();
                page = 1;
                getData();
            }
        });
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void start() {
        viewBinding.suggestionsTitle.contentTitle.setText("我的反馈");
        viewBinding.suggestionsTitle.leftImage.setImageDrawable(getDrawable(R.drawable.back_cliener));
        viewBinding.suggestionsTitle.titleleft.setOnClickListener(V -> finish());
        viewBinding.suggestionsRslRefresh.autoRefresh();
    }

    private FeedbackBean getErrorBean(int type, String msg) {
        FeedbackBean bean = new FeedbackBean();
        bean.setBaseType(type);
        bean.setBaseMessage(msg);
        return bean;
    }

    private void getData() {
        ApiModel.request("getMyFeedback", RetrofitService.getBaseInstance().feedbackList(page, pageSize),
                new BaseRequestCallback<BaseRequestModel<PagingModel<FeedbackBean>>>() {
                    @Override
                    public void onRequestFailure(String msg) {
                        LogUtils.e(TAG, msg);
                        if (viewBinding.suggestionsRslRefresh.isRefreshing()){
                            handRefreshData(null,-1,msg);
                        }
                        if (viewBinding.suggestionsRslRefresh.isLoading()){
                            handLoadMoreData(null);
                        }
                        ToastUtil.centered(getContext(),msg);
                    }

                    @Override
                    public void onRequestSuccess(BaseRequestModel<PagingModel<FeedbackBean>> result) {
                        LogUtils.w(TAG, result.toString());
                        if (result.getCode() == 20041) {
                            PagingModel<FeedbackBean> data = result.getData();
                            total = data.getCounts();
                            List<FeedbackBean> items = data.getItems();
                            if (viewBinding.suggestionsRslRefresh.isRefreshing()){
                                handRefreshData(items,0,"暂无反馈");
                            }
                            if (viewBinding.suggestionsRslRefresh.isLoading()){
                                handLoadMoreData(items);
                            }
                            if (page*pageSize>=total){
                                viewBinding.suggestionsRslRefresh.finishLoadMoreWithNoMoreData();
                            }
                        }else {
                            if (viewBinding.suggestionsRslRefresh.isRefreshing()){
                                handRefreshData(null,-1,result.getMsg());
                            }
                            if (viewBinding.suggestionsRslRefresh.isLoading()){
                                handLoadMoreData(null);
                            }
                            ToastUtil.centered(getContext(),result.getMsg());
                        }
                    }
                });
    }

    private void initData(List<FeedbackBean> items) {
        if (adapter == null){
            adapter = new FeedBackListAdapter(getContext(),items);
            viewBinding.suggestionsRvList.setAdapter(adapter);
            adapter.setListClickListener(new SimpleListClickListener() {
                @Override
                public void onSingleClick(View v, int pointer) {
                    if (adapter.getItemViewType(pointer)!=1)return;
                    Intent intent = new Intent(getContext(),SuggestionsDetailsActivity.class);
                    intent.putExtra("dataModel",adapter.getDataItem(pointer));
                    startActivity(intent);
                }
            });
        }else {
            adapter.upData(items);
        }
    }

    private void handRefreshData(List<FeedbackBean> items,int type,String msg) {
        if (items == null || items.isEmpty()) {
            items = new ArrayList<>();
            items.add(getErrorBean(0, "暂无反馈"));
        }else {
            for (FeedbackBean b : items) {
                b.setBaseType(1);
            }
        }
        viewBinding.suggestionsRslRefresh.finishRefresh();
        initData(items);
    }

    private void handLoadMoreData(List<FeedbackBean> items) {
        viewBinding.suggestionsRslRefresh.finishLoadMore();
        if (items!=null&&!items.isEmpty()){
            adapter.addData(items);
        }
    }
}