package com.gh.gamecenter.message;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.common.baselist.ListFragment;
import com.gh.gamecenter.common.baselist.LoadType;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.entity.MessageEntity;
import com.halo.assistant.HaloApp;

/**
 * Created by khy on 24/03/18.
 * 不需要未读机制
 */

public class MessageNormalFragment extends ListFragment<MessageEntity, MessageNormalViewModel> {
    public final static String MESSAGE_TYPE_VOTE = "vote";
    public final static String MESSAGE_TYPE_INVITE = "invite";

    private MessageNormalAdapter mAdapter;

    private MessageUnreadViewModel mUnreadViewModel;

    private String mMessageType;
    private String mTitle;
    private String mOuterInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (getArguments() == null)
            throw new NullPointerException(MessageFragment.class.getSimpleName() + " Arguments is not null");
        mMessageType = getArguments().getString(EntranceConsts.KEY_MESSAGE_TYPE);
        mOuterInfo = getArguments().getString(EntranceConsts.KEY_OUTER_INFO);
        super.onCreate(savedInstanceState);

        mUnreadViewModel = ViewModelProviders.of(this, new MessageUnreadViewModel.Factory(HaloApp.getInstance().getApplication()))
                .get(MessageUnreadViewModel.class);
    }

    @Override
    protected MessageNormalViewModel provideListViewModel() {
        MessageNormalViewModel.Factory factory = new MessageNormalViewModel.Factory(HaloApp.getInstance().getApplication(), mMessageType);
        return ViewModelProviders.of(this, factory).get(MessageNormalViewModel.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (MESSAGE_TYPE_INVITE.equals(mMessageType)) {
            mTitle = "邀请";
        } else if (MESSAGE_TYPE_VOTE.equals(mMessageType)) {
            mTitle = "赞同";
        }
        setNavigationTitle(mTitle);
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        CustomDividerItemDecoration decoration = new CustomDividerItemDecoration(requireContext(), false, false, true, false);
        decoration.setDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.divider_item_line_space_16_h_1px));
        mItemDecoration = decoration;
        return mItemDecoration;
    }


    @Override
    public void onLoadDone() {
        super.onLoadDone();
        if (MESSAGE_TYPE_INVITE.equals(mMessageType)) {
            mUnreadViewModel.markRead(MessageUnreadViewModel.ReadType.INVITE);
        } else {
            mUnreadViewModel.markRead(MessageUnreadViewModel.ReadType.VOTE);
        }
    }

    @Override
    protected ListAdapter provideListAdapter() {
        return mAdapter == null ? mAdapter = new MessageNormalAdapter(
                getContext(),
                this,
                mEntrance,
                mOuterInfo,
                mListViewModel) : mAdapter;
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        if (view.getId() == R.id.footerview_item) {
            if (mAdapter.isNetworkError()) {
                mListViewModel.load(LoadType.RETRY);
            }
            return;
        }
        MessageEntity entity = (MessageEntity) data;
        String path = "我的光环-消息中心-" + mTitle;
        MessageItemViewHolder.messageItemClickSkip(view, entity, mEntrance, mOuterInfo, path);
//        if (!entity.getRead()) {
//            mListViewModel.postMessageRead(entity.getId(), entity.getType());
//            mAdapter.notifyItemChanged(position);
//        }
    }
}
