package com.gh.gamecenter.qa.myqa;

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

import androidx.recyclerview.widget.RecyclerView;

import com.gh.common.util.NewFlatLogUtils;
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.R;
import com.gh.gamecenter.common.viewholder.FooterViewHolder;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding;
import com.gh.gamecenter.forum.home.ForumArticleAskItemViewHolder;
import com.gh.gamecenter.qa.entity.AnswerEntity;
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity;

import org.jetbrains.annotations.Nullable;

import kotlin.Pair;

/**
 * Created by khy on 20/12/17.
 */

class MyAnswerAdapter extends ListAdapter<AnswerEntity> implements ISyncAdapterHandler {

    private OnListClickListener mListClickListener;
    private String mEntrance;

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

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case ItemViewType.ITEM_FOOTER:
                view = mLayoutInflater.inflate(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 ForumArticleAskItemViewHolder(CommunityAnswerItemBinding.bind(view));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ItemViewType.ITEM_BODY:
                ForumArticleAskItemViewHolder answerViewHolder = ((ForumArticleAskItemViewHolder) holder);
                AnswerEntity entity = mEntityList.get(position);

                answerViewHolder.bindForumAnswerItem(entity, mEntrance, "", position);

                CommunityAnswerItemBinding binding = answerViewHolder.getBinding();
                binding.userIcon.setVisibility(View.GONE);
                binding.userName.setVisibility(View.GONE);
                binding.userBadgeIcon.setVisibility(View.GONE);
                binding.userBadgeName.setVisibility(View.GONE);
                binding.concernBtn.setVisibility(View.GONE);
                binding.timeContainer.setVisibility(View.GONE);
                binding.userIcon.setVisibility(View.GONE);
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) binding.questionTitle.getLayoutParams();
                params.topMargin = DisplayUtils.dip2px(20F);
                binding.questionTitle.setLayoutParams(params);

                holder.itemView.setOnClickListener(v -> {
                    if (mEntrance.equals("我的发布")) {
                        NewFlatLogUtils.logHaloSelfPublishContent("回答", "卡片内容");
                    }
                    mContext.startActivity(NewQuestionDetailActivity.getCommentIntent(mContext,
                            entity.getQuestions().getId(), entity.getId() != null ? entity.getId() : "", mEntrance, "我的光环-我的问答-我的回答"));
                });
                answerViewHolder.getCommentCount().setVisibility(View.INVISIBLE);

                break;
            case ItemViewType.ITEM_FOOTER:
                FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
                footerViewHolder.initItemPadding();
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver);
                break;
        }
    }

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

    @Nullable
    @Override
    public Pair<String, Object> getSyncData(int position) {
        if (position >= mEntityList.size()) return null;
        AnswerEntity entity = mEntityList.get(position);
        return new Pair(entity.getId(), entity);
    }
}
