package com.gh.gamecenter.message;

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

import androidx.recyclerview.widget.RecyclerView;

import com.gh.gamecenter.common.callback.OnListClickListener;
import com.gh.gamecenter.common.constant.ItemViewType;
import com.gh.gamecenter.common.utils.DialogHelper;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.viewholder.FooterViewHolder;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.databinding.MessageItemBinding;
import com.gh.gamecenter.entity.MessageEntity;

/**
 * Created by khy on 23/03/18.
 */

public class MessageNormalAdapter extends ListAdapter<MessageEntity> {

    private OnListClickListener mClickListener;

    private MessageNormalViewModel mViewModel;
    private String mEntrance;
    private String mOuterInfo;

    public MessageNormalAdapter(Context context,
                                OnListClickListener clickListener,
                                String entrance,
                                String outerInfo,
                                MessageNormalViewModel viewModel) {
        super(context);
        mClickListener = clickListener;
        mEntrance = entrance;
        mOuterInfo = outerInfo;
        mViewModel = viewModel;
    }

    @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, mClickListener);
            case ItemViewType.ITEM_BODY:
                MessageItemBinding binding = MessageItemBinding.inflate(mLayoutInflater, parent, false);
                return new MessageItemViewHolder(binding, mClickListener, mOuterInfo);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ItemViewType.ITEM_BODY:
                MessageItemViewHolder viewHolder = (MessageItemViewHolder) holder;
                MessageEntity entity = mEntityList.get(position);
                entity.setRead(true);
                viewHolder.setMessageItem(entity, mContext, mEntrance);
                viewHolder.itemView.setOnLongClickListener(v -> {
                    String mtaKey;
                    if (MessageNormalFragment.MESSAGE_TYPE_INVITE.equals(mViewModel.getMessageType())) {
                        mtaKey = "邀请列表-删除";
                    } else {
                        mtaKey = "赞同列表-删除";
                    }
                    DialogHelper.showDialog(
                            mContext,
                            "删除消息",
                            "消息删除将不可恢复，确定删除吗？",
                            "确定",
                            "取消",
                            () -> mViewModel.deleteMessage(entity.getId()),
                            () -> {
                            },
                            true,
                            "消息中心",
                            mtaKey);
                    return false;
                });
                break;
            case ItemViewType.ITEM_FOOTER:
                ((FooterViewHolder) holder).initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver);
                ((FooterViewHolder) holder).initItemPadding();
                break;
        }
    }

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

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