package com.gh.gamecenter.message;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.gh.gamecenter.entity.AddonsUnreadEntity;
import com.gh.gamecenter.feature.entity.MessageUnreadCount;
import com.gh.gamecenter.feature.entity.MessageUnreadEntity;


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

public class MessageUnreadViewModel extends AndroidViewModel {

    public enum ReadType {
        DEFAULT,
        VOTE,
        INVITE,
        SERVICE,
        FANS
    }

    private MediatorLiveData<MessageUnreadEntity> mUnreadLiveData;
    private MutableLiveData<MessageUnreadCount> mMessageUnreadCountLiveData;
    private MutableLiveData<Integer> mMessageCenterUnreadCountLiveData;

    public MessageUnreadViewModel(@NonNull Application application) {
        super(application);
        mUnreadLiveData = MessageUnreadRepository.INSTANCE.getUnreadLiveData();
        mMessageUnreadCountLiveData = MessageUnreadRepository.INSTANCE.getMessageUnreadCountLiveData();
        mMessageCenterUnreadCountLiveData = MessageUnreadRepository.INSTANCE.getMessageCenterUnreadCountLiveData();
    }

    public MutableLiveData<AddonsUnreadEntity> getAddonsUnreadLiveData() {
        return MessageUnreadRepository.INSTANCE.getAddonsUnread();
    }

    public MutableLiveData<Boolean> getUnreadMessageTotalLiveData() {
        return MessageUnreadRepository.INSTANCE.getUnreadMessageTotal();
    }

    public MutableLiveData<Boolean> getZixunConcernLiveData() {
        return MessageUnreadRepository.INSTANCE.getZixunConcern();
    }

    public MediatorLiveData<MessageUnreadEntity> getLiveData() {
        return mUnreadLiveData;
    }

    public MutableLiveData<MessageUnreadCount> getMessageUnreadCountLiveData() {
        return mMessageUnreadCountLiveData;
    }

    public MutableLiveData<Integer> getMessageCenterUnreadCountLiveData() {
        return mMessageCenterUnreadCountLiveData;
    }

    public void retry() {
        MessageUnreadRepository.INSTANCE.loadMessageUnreadData();
    }

    public boolean isFirstTimeInit() {
        return MessageUnreadRepository.INSTANCE.getFirstTimeInit();
    }

    public void flagFirstTimeInit(boolean flag) {
        MessageUnreadRepository.INSTANCE.setFirstTimeInit(flag);
    }

    public void clean() {
        mUnreadLiveData.postValue(null);
    }

    public void markRead(ReadType readType) {
        MessageUnreadEntity cacheUnreadData = MessageUnreadRepository.INSTANCE.getUnreadLiveData().getValue();
        if (cacheUnreadData == null) return;
        boolean isChange = false;
        switch (readType) {
            case SERVICE:
                if (cacheUnreadData.getService() != 0) {
                    cacheUnreadData.setService(0);
                    isChange = true;
                }
                break;
            case VOTE:
                if (cacheUnreadData.getVoteCount() != 0) {
                    cacheUnreadData.resetVote();
                    isChange = true;
                }
                break;
            case INVITE:
                if (cacheUnreadData.getInvited() + cacheUnreadData.getSystemInvited() != 0) {
                    cacheUnreadData.setInvited(0);
                    cacheUnreadData.setSystemInvited(0);
                    isChange = true;
                }
                break;
            case DEFAULT:
                if (cacheUnreadData.getAnswer() + cacheUnreadData.getAnswerComment() + cacheUnreadData.getReply() +
                        cacheUnreadData.getFollowQuestion() + cacheUnreadData.getReplyAnswerComment() +
                        cacheUnreadData.getCommunityArticleComment() + cacheUnreadData.getReplyCommunityArticleComment() != 0) {
                    cacheUnreadData.setAnswer(0);
                    cacheUnreadData.setAnswerComment(0);
                    cacheUnreadData.setReply(0);
                    cacheUnreadData.setFollowQuestion(0);
                    cacheUnreadData.setReplyAnswerComment(0);
                    cacheUnreadData.setCommunityArticleComment(0);
                    cacheUnreadData.setReplyCommunityArticleComment(0);
                    isChange = true;
                }
                break;
            case FANS:
                if (cacheUnreadData.getFans() != 0) {
                    cacheUnreadData.setFans(0);
                    isChange = true;
                }
                break;
        }

        if (isChange) {
            // 重新统计总数
            cacheUnreadData.setTotal(cacheUnreadData.getVoteCount() +
                    cacheUnreadData.getService() + cacheUnreadData.getAnswer() + cacheUnreadData.getReply() +
                    cacheUnreadData.getFollowQuestion() + cacheUnreadData.getReplyAnswerComment() + cacheUnreadData.getAnswerComment() +
                    cacheUnreadData.getSystemInvited() + cacheUnreadData.getFans());
            MessageUnreadRepository.INSTANCE.getUnreadLiveData().postValue(cacheUnreadData);
        }
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        @NonNull
        private final Application mApplication;

        public Factory(@NonNull Application application) {
            mApplication = application;
        }

        @Override
        public <T extends ViewModel> T create(Class<T> modelClass) {
            //noinspection unchecked
            return (T) new MessageUnreadViewModel(mApplication);
        }
    }
}
