package com.gh.gamecenter.qa.subject;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.gh.gamecenter.R;
import com.gh.gamecenter.common.viewholder.FooterViewHolder;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.common.callback.OnListClickListener;
import com.gh.gamecenter.common.constant.ItemViewType;
import com.gh.gamecenter.common.syncpage.ISyncAdapterHandler;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.databinding.AskSubjectTopItemBinding;
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding;
import com.gh.gamecenter.common.entity.CommunityEntity;
import com.gh.gamecenter.qa.answer.CommunityAnswerItemViewHolder;
import com.gh.gamecenter.qa.answer.detail.SimpleAnswerDetailActivity;
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity;
import com.gh.gamecenter.feature.entity.AnswerEntity;
import com.gh.gamecenter.qa.entity.AskSubjectEntity;
import com.gh.gamecenter.feature.entity.Questions;
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity;

import org.jetbrains.annotations.Nullable;

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

import kotlin.Pair;

/**
 * Created by khy on 1/04/18.
 */

public class AskSubjectAdapter extends ListAdapter<AnswerEntity> implements ISyncAdapterHandler {

    private OnListClickListener mListClickListener;

    private AskSubjectEntity mSubjectEntity;

    private String mEntrance;

    public AskSubjectAdapter(Context context, OnListClickListener listClickListener, String entrance) {
        super(context);
        mListClickListener = listClickListener;
        mEntrance = entrance;
    }

    @Override
    protected void setListData(List<AnswerEntity> updateData) {
        if (mSubjectEntity == null) {
            notifyDataSetChanged();
            return;
        }
        int oldSize = mEntityList == null || mEntityList.isEmpty() ? 0 : mEntityList.size() + TOP_ITEM_COUNT;
        mEntityList = new ArrayList<>(updateData);
        if (oldSize == 0 || oldSize > updateData.size()) {
            notifyDataSetChanged();
        } else {
            notifyItemRangeInserted(oldSize, updateData.size() + TOP_ITEM_COUNT - oldSize);
        }
    }

    public void setSubjectEntity(AskSubjectEntity subjectEntity) {
        mSubjectEntity = subjectEntity;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case ItemViewType.ITEM_HEADER:
                view = mLayoutInflater.inflate(R.layout.ask_subject_top_item, parent, false);
                AskSubjectTopItemBinding binding = AskSubjectTopItemBinding.bind(view);
                return new AskSubjectViewHolder(binding, mListClickListener);
            case ItemViewType.ITEM_FOOTER:
                view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false);
                return new FooterViewHolder(view, mListClickListener);
            case ItemViewType.ITEM_BODY:
                view = mLayoutInflater.inflate(R.layout.community_answer_item, parent, false);
                return new CommunityAnswerItemViewHolder(CommunityAnswerItemBinding.bind(view));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ItemViewType.ITEM_HEADER:
                AskSubjectViewHolder subjectViewHolder = (AskSubjectViewHolder) holder;
                ImageUtils.display(subjectViewHolder.mBinding.askSubjectTopIcon, mSubjectEntity.getIcon());
                subjectViewHolder.mBinding.askSubjectTopTitle.setText(mSubjectEntity.getTitle());
                subjectViewHolder.mBinding.askSubjectTopDes.setText(mSubjectEntity.getDescription());
                subjectViewHolder.mBinding.askSubjectTopDes.setTextColor(ContextCompat.getColor(subjectViewHolder.mBinding.getRoot().getContext(), com.gh.gamecenter.common.R.color.title));
                subjectViewHolder.mBinding.askSubjectTopTag.setTextColor(ContextCompat.getColor(subjectViewHolder.mBinding.getRoot().getContext(), com.gh.gamecenter.common.R.color.title));
                if (mEntityList == null || mEntityList.isEmpty()) {
                    subjectViewHolder.mBinding.askSubjectTopTag.setVisibility(View.GONE);
                } else {
                    subjectViewHolder.mBinding.askSubjectTopTag.setVisibility(View.VISIBLE);
                }
                break;
            case ItemViewType.ITEM_FOOTER:
                FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
                footerViewHolder.initItemPadding();
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver);
                break;
            case ItemViewType.ITEM_BODY:
                String path = "问答专题详情";

                int index = position - ListAdapter.TOP_ITEM_COUNT;
                if (index != -1) {
                    AnswerEntity entity = mEntityList.get(index);
                    CommunityAnswerItemViewHolder viewHolder = (CommunityAnswerItemViewHolder) holder;
                    CommunityAnswerItemBinding binding = viewHolder.getBinding();
                    if ("community_article".equals(entity.getType())) {
                        Questions questions = new Questions();
                        questions.setTitle(entity.getTitle());
                        entity.setQuestions(questions);
                    }

                    viewHolder.bindAnswerItem(entity, mEntrance, path);
                    binding.title.setOnClickListener(v -> {
                        if ("answer".equals(entity.getType())) {
                            Questions questions = entity.getQuestions();
                            mContext.startActivity(NewQuestionDetailActivity.getIntent(mContext, questions.getId(), mEntrance, path, ""));
                        } else {
                            mContext.startActivity(ArticleDetailActivity.getIntent(mContext, new CommunityEntity(entity.getArticleCommunityId(), ""), entity.getId(), mEntrance, path, null, ""));
                        }
                    });

                    holder.itemView.setOnClickListener(v -> {
                        if ("answer".equals(entity.getType())) {
                            mContext.startActivity(SimpleAnswerDetailActivity.getIntent(mContext, entity.getId(), mEntrance, path));
                        } else {
                            mContext.startActivity(ArticleDetailActivity.getIntent(mContext, new CommunityEntity(entity.getArticleCommunityId(), ""), entity.getId(), mEntrance, path, null, ""));
                        }
                    });
                }
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return ItemViewType.ITEM_HEADER;
        if (position == getItemCount() - 1) return ItemViewType.ITEM_FOOTER;
        return ItemViewType.ITEM_BODY;
    }

    @Override
    public int getItemCount() {
        if (mSubjectEntity == null) return 0;
        return mEntityList == null ? TOP_ITEM_COUNT + FOOTER_ITEM_COUNT : mEntityList.size() + FOOTER_ITEM_COUNT + TOP_ITEM_COUNT;
    }

    @Nullable
    @Override
    public Pair<String, Object> getSyncData(int position) {
        // position:0 -> head
        int index = position - ListAdapter.TOP_ITEM_COUNT;
        if (index < 0 || index >= mEntityList.size()) return null;

        AnswerEntity entity = mEntityList.get(index);
        return new Pair(entity.getId(), entity);
    }
}
