package com.gh.gamecenter.message;

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.common.callback.OnListClickListener;
import com.gh.gamecenter.common.constant.ItemViewType;
import com.gh.common.databind.BindingAdapters;
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.databinding.MessageItemTopBinding;
import com.gh.gamecenter.entity.MessageEntity;
import com.gh.gamecenter.entity.MessageUnreadEntity;

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

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

public class MessageAdapter extends ListAdapter<MessageEntity> {

    private OnListClickListener mClickListener;

    private MessageUnreadEntity mUnreadEntity;

    private MessageNormalViewModel mViewModel;

    private String mEntrance;

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

    @Override
    protected void setListData(List<MessageEntity> updateData) {
        int oldSize = TOP_ITEM_COUNT;
        if (mEntityList != null && mEntityList.size() > 0) {
            oldSize += mEntityList.size();
        }
        mEntityList = new ArrayList<>(updateData);
        if (oldSize == TOP_ITEM_COUNT || oldSize > updateData.size()) {
            notifyDataSetChanged();
        } else {
            notifyItemRangeInserted(oldSize, updateData.size() + TOP_ITEM_COUNT - oldSize);
        }
    }

    void setMessageUnreadData(MessageUnreadEntity unreadEntity) {
        mUnreadEntity = unreadEntity;
        notifyItemChanged(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ItemViewType.ITEM_HEADER:
                MessageItemTopBinding topBinding = MessageItemTopBinding.inflate(mLayoutInflater, parent, false);
                return new MessageTopViewHolder(topBinding, mClickListener);
            case ItemViewType.ITEM_FOOTER:
                View 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, "消息_一级列表");
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ItemViewType.ITEM_HEADER:
                MessageTopViewHolder topViewHolder = (MessageTopViewHolder) holder;
                topViewHolder.mBinding.messageInviteName.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
                topViewHolder.mBinding.messageVoteName.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
                topViewHolder.mBinding.messageServiceName.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
                topViewHolder.mBinding.messageVote.setBackground(ContextCompat.getDrawable(mContext, R.drawable.reuse_listview_item_style));
                topViewHolder.mBinding.messageService.setBackground(ContextCompat.getDrawable(mContext, R.drawable.reuse_listview_item_style));
                topViewHolder.mBinding.messageInvite.setBackground(ContextCompat.getDrawable(mContext, R.drawable.reuse_listview_item_style));

                if (mUnreadEntity != null) {
                    BindingAdapters.setMessageUnread(topViewHolder.mBinding.unreadVote, mUnreadEntity.getVoteCount());
                    BindingAdapters.showHide(topViewHolder.mBinding.unreadVote, mUnreadEntity.getVoteCount() != 0);

                    BindingAdapters.setMessageUnread(topViewHolder.mBinding.unreadInvite, mUnreadEntity.getInvited() + mUnreadEntity.getSystemInvited());
                    BindingAdapters.showHide(topViewHolder.mBinding.unreadInvite, mUnreadEntity.getInvited() + mUnreadEntity.getSystemInvited() != 0);

                    BindingAdapters.setMessageUnread(topViewHolder.mBinding.unreadService, mUnreadEntity.getService());
                    BindingAdapters.showHide(topViewHolder.mBinding.unreadService, mUnreadEntity.getService() != 0);
                }
                break;
            case ItemViewType.ITEM_BODY:
                MessageItemViewHolder viewHolder = (MessageItemViewHolder) holder;
                MessageEntity entity = mEntityList.get(position - TOP_ITEM_COUNT);
                viewHolder.setMessageItem(entity, mContext, mEntrance);
                viewHolder.itemView.setOnLongClickListener(v -> {
                    DialogHelper.showDialog(
                            mContext,
                            "删除消息",
                            "消息删除将不可恢复，确定删除吗？",
                            "确定",
                            "取消",
                            () -> mViewModel.deleteMessage(entity.getId()),
                            () -> {
                            },
                            true,
                            "消息中心",
                            "消息列表-删除");
                    return false;
                });
                break;
            case ItemViewType.ITEM_FOOTER:
                FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver);
                footerViewHolder.initItemPadding();
                break;
        }
    }

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

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