package com.gh.gamecenter.message;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.gh.common.util.NewLogUtils;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.MessageInviteActivity;
import com.gh.gamecenter.MessageKeFuActivity;
import com.gh.gamecenter.MessageVoteActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.baselist.ListFragment;
import com.gh.gamecenter.common.baselist.LoadStatus;
import com.gh.gamecenter.common.baselist.LoadType;
import com.gh.gamecenter.entity.MessageEntity;
import com.gh.gamecenter.entity.MessageUnreadEntity;
import com.halo.assistant.HaloApp;

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

public class MessageFragment extends ListFragment<MessageEntity, MessageNormalViewModel> {

    private MessageAdapter mAdapter;

    private MessageUnreadViewModel mUnreadViewModel;

    private MessageUnreadEntity mUnreadEntity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setNavigationTitle(getString(R.string.title_message_center));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUnreadViewModel = ViewModelProviders.of(this, new MessageUnreadViewModel.Factory(HaloApp.getInstance().getApplication()))
                .get(MessageUnreadViewModel.class);
        if (savedInstanceState != null) {
            mUnreadViewModel.retry();
        }
        mUnreadViewModel.getLiveData().observe(this,
                messageUnread -> {
                    mUnreadEntity = messageUnread;
                    provideListAdapter().setMessageUnreadData(messageUnread);
                });
    }

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

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

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

    @Override
    public void onLoadEmpty() {
        super.onLoadDone();
        mAdapter.loadChange(LoadStatus.LIST_OVER);
        mReuseNoData.setVisibility(View.VISIBLE);
        mReuseNoData.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.transparent));
    }

    @Override
    public void onLoadError() {
        super.onLoadDone();
        mAdapter.loadChange(LoadStatus.LIST_FAILED);
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        switch (view.getId()) {
            case R.id.footerview_item:
                if (mAdapter.isNetworkError()) {
                    mListViewModel.load(LoadType.RETRY);
                }
                return;
            case R.id.message_vote:
                MtaHelper.onEvent("消息中心", "二级入口", "赞同");
                NewLogUtils.logMessageInformTopIconClick(mUnreadEntity.getVoteCount() != 0, "赞同");
                startActivity(MessageVoteActivity.getIntent(getContext(), MessageNormalFragment.MESSAGE_TYPE_VOTE, "赞同_二级列表", mEntrance));
                return;
            case R.id.message_invite:
                MtaHelper.onEvent("消息中心", "二级入口", "邀请");
                NewLogUtils.logMessageInformTopIconClick(mUnreadEntity.getInvited() + mUnreadEntity.getSystemInvited() != 0, "邀请");
                startActivity(MessageInviteActivity.getIntent(getContext(), MessageNormalFragment.MESSAGE_TYPE_INVITE, "邀请_二级列表", mEntrance));
                return;
            case R.id.message_service:
                MtaHelper.onEvent("消息中心", "二级入口", "系统");
                NewLogUtils.logMessageInformTopIconClick(mUnreadEntity.getService() != 0, "系统");
                startActivity(MessageKeFuActivity.getIntent(getContext(), mEntrance));
                return;
        }

        MessageEntity entity = (MessageEntity) data;
        String path = "我的光环-消息中心-列表";
        MessageItemViewHolder.messageItemClickSkip(view, entity, mEntrance, "消息_一级列表", path);
        if (!entity.getRead()) {
            mListViewModel.postMessageRead(entity.getId(), entity.getType());
        }
    }
}
