package com.cooleshow.student.ui.main;

import android.graphics.Rect;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseFragment;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.cooleshow.student.adapter.HomeHotNewsAdapter;
import com.cooleshow.student.bean.AppHomeBean;
import com.cooleshow.student.bean.HomeHotNewsBean;
import com.cooleshow.student.databinding.FragmentHomeHotNewsLayoutBinding;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Author by pq, Date on 2022/8/31.
 */
public class HotNewsFragment extends BaseFragment<FragmentHomeHotNewsLayoutBinding> {

    private HomeHotNewsAdapter mHomeHotNewsAdapter;
    private OnEventListener onEventListener;
    private boolean hasNext = true;
    private int currentPage = 1;

    @Override
    protected FragmentHomeHotNewsLayoutBinding getLayoutView() {
        return FragmentHomeHotNewsLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView(View rootView) {
        mHomeHotNewsAdapter = getAdapter();
        EmptyViewLayout emptyViewLayout = new EmptyViewLayout(getContext());
        emptyViewLayout.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无内容");
        mHomeHotNewsAdapter.setEmptyView(emptyViewLayout);
        mViewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mViewBinding.recyclerView.setAdapter(mHomeHotNewsAdapter);
        mViewBinding.recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                //每行都只有2个
                int childLayoutPosition = parent.getChildLayoutPosition(view);
                if (childLayoutPosition == 0) {
                    outRect.top = SizeUtils.dp2px(5);
                } else {
                    outRect.top = SizeUtils.dp2px(0);
                }
            }
        });
        mHomeHotNewsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position < mHomeHotNewsAdapter.getData().size()) {
                    HomeHotNewsBean.RowsBean item = mHomeHotNewsAdapter.getData().get(position);
                    String url;
                    if (TextUtils.equals(item.linkType, "OUT")) {
                        url = item.linkUrl;
                    } else {
                        url = WebConstants.STUDENT_SPECIAL_DETAIL + item.id;
                    }
                    ARouter.getInstance()
                            .build(RouterPath.WebCenter.ACTIVITY_HTML)
                            .withString(WebConstants.WEB_URL, url)
                            .navigation();
                }
            }
        });
        mHomeHotNewsAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //上拉加载
                if (hasNext) {
                    currentPage++;
                    queryMoreData();
                } else {
                    mHomeHotNewsAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void queryMoreData() {
        if (onEventListener != null) {
            onEventListener.getNewsData(currentPage);
        }
    }

    public void refresh() {
        if (onEventListener != null) {
            currentPage = 1;
            onEventListener.getNewsData(currentPage);
        }
    }

    public void setData(int page, HomeHotNewsBean data) {
        if (getAdapter() != null) {
            if (page == 1) {
                mHomeHotNewsAdapter.getData().clear();
                mHomeHotNewsAdapter.notifyDataSetChanged();
                if (data != null && data.rows != null && data.rows.size() > 0) {
                    checkHasNext(data.rows.size());
                    mHomeHotNewsAdapter.setNewInstance(data.rows);
                } else {
                    //showEmpty
                }
            } else {
                if (data != null && data.rows != null && data.rows.size() > 0) {
                    mHomeHotNewsAdapter.getLoadMoreModule().loadMoreComplete();
                    checkHasNext(data.rows.size());
                    mHomeHotNewsAdapter.addData(data.rows);
                } else {
                    mHomeHotNewsAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }
        }
    }

    public void getHotNewsError(int page) {
        if (page > 1) {
            if (mHomeHotNewsAdapter != null) {
                currentPage--;
                mHomeHotNewsAdapter.getLoadMoreModule().loadMoreFail();
            }
        }
    }

    private HomeHotNewsAdapter getAdapter() {
        if (mHomeHotNewsAdapter == null) {
            mHomeHotNewsAdapter = new HomeHotNewsAdapter();
        }
        return mHomeHotNewsAdapter;
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.onEventListener = onEventListener;
    }

    public interface OnEventListener {
        void getNewsData(int page);
    }

    private void checkHasNext(int dataSize) {
        hasNext = dataSize >= Constants.DEFAULT_DATA_SIZE;
    }
}
