package com.datarecovery.master.module.homepage;

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.common.runtime.ActivityUtil;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.data.repositories.ConfigRepository;
import com.datarecovery.master.data.repositories.DeviceFuncRepository;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.audiorecover.AudioRecoverActivity;
import com.datarecovery.master.module.filerecover.FileRecoverActivity;
import com.datarecovery.master.module.imgrecover.ImageRecoverActivity;
import com.datarecovery.master.module.member.MemberActivity;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.module.videorecover.VideoRecoverActivity;
import com.datarecovery.master.module.wxrecover.WeChatRecoverActivity;
import com.datarecovery.master.utils.Maps;
import com.datarecovery.master.utils.ReportUtil;

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

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

import android.util.Pair;

import androidx.lifecycle.LiveData;


@HiltViewModel
public class HomePageViewModel extends BaseViewModel {


    private final List<Pair<CharSequence, CharSequence>> informationList = new ArrayList<>();
    private final List<FunctionBean> functionList = new ArrayList<>();

    private final SingleLiveEvent<?> requestManagePermission = new SingleLiveEvent<>();
    private final SingleLiveEvent<?> requestAndroidDataPermission = new SingleLiveEvent<>();
    private final DeviceFuncRepository deviceFuncRepository;


    @Inject
    public HomePageViewModel(DeviceFuncRepository deviceFuncRepository) {
        this.deviceFuncRepository = deviceFuncRepository;
        initList();
    }

    public List<FunctionBean> getFunctionList() {
        return functionList;
    }

    public List<Pair<CharSequence, CharSequence>> getInformationList() {
        return informationList;
    }

    private void initList() {
        informationList.add(new Pair<>("163*****974 购买了微信消息恢复", "2分钟前"));
        informationList.add(new Pair<>("183*****823 购买了图片恢复", "4分钟前"));
        informationList.add(new Pair<>("172*****194 购买了微信好友恢复", "5分钟前"));
        informationList.add(new Pair<>("193*****347 购买了微信消息恢复", "9分钟前"));
        informationList.add(new Pair<>("153*****912 购买了音频恢复", "11分钟前"));
        informationList.add(new Pair<>("159*****864 购买了视频恢复", "15分钟前"));

        functionList.add(new FunctionBean(FunctionBean.FILE_RECOVERY, R.string.home_page_file_recovery, R.drawable.icon_home_page_file_recovery));
        functionList.add(new FunctionBean(FunctionBean.VIDEO_RECOVERY, R.string.home_page_video_recovery, R.drawable.icon_home_page_video_recovery));
        functionList.add(new FunctionBean(FunctionBean.AUDIO_RECOVERY, R.string.home_page_audio_recovery, R.drawable.icon_home_page_audio_recovery));
        functionList.add(new FunctionBean(FunctionBean.IMG_CLEARING, R.string.home_page_img_clearing, R.drawable.icon_home_page_img_clearing));
    }

    public LiveData<?> getRequestManagePermission() {
        return requestManagePermission;
    }

    public LiveData<?> getRequestAndroidDataPermission() {
        return requestAndroidDataPermission;
    }

    public void onWxMessageRecoveryClick() {
        isHaveAuths(MemberType.APP_WX_MESSAGE_RECOVER, isTrial -> WeChatRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_WX_MESSAGE_RECOVER));
    }

    public void onWxFriendRecoveryClick() {
        isHaveAuths(MemberType.APP_WX_FRIEND_RECOVER, isTrial -> WeChatRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_WX_FRIEND_RECOVER));
    }

    public void onImgRecoveryClick() {
        isHaveAuths(MemberType.APP_IMAGE_RECOVER, isTrial -> ImageRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_IMAGE_RECOVER, isTrial));
    }

    public void isHaveAuths(@MemberType String type, NextStepCallback stepCallback) {
        reportFirstClickFunction(type);
        if (deviceFuncRepository.isHaveAuth(type)) {
            if (stepCallback != null) stepCallback.onNextStep(false);
        } else if (ConfigRepository.isConformTrialAuths(type)) {
            if (stepCallback != null) stepCallback.onNextStep(true);
        } else {
            MemberActivity.start(ActivityUtil.getTopActivity(), type);
        }
    }

    public void reportFirstClickFunction(@MemberType String type) {
        if (ConfigRepository.isIsOpenTrialMembership()) {
            if (ReportUtil.isRecordOneEvent(EventId.hf1001116)) {
                EventHelper.report(EventId.hf1001130, Maps.asMap(EventId.EVENT_ID, ReportUtil.getReportId(type)));
                return;
            }
            EventHelper.report(EventId.hf1001116, Maps.asMap(EventId.EVENT_ID, ReportUtil.getReportId(type)));
        } else {
            if (ReportUtil.isRecordOneEvent(EventId.hf200008)) {
                return;
            }
            EventHelper.report(EventId.hf200008, Maps.asMap(EventId.EVENT_ID, ReportUtil.getReportId(type)));
        }
    }


    public void clickItemFunction(FunctionBean bean) {
        if (bean == null) {
            return;
        }
        switch (bean.getFunctionId()) {
            case FunctionBean.AUDIO_RECOVERY:
                isHaveAuths(MemberType.APP_AUDIO_RECOVER, isTrial -> AudioRecoverActivity.start(ActivityUtil.getTopActivity(), isTrial));
                break;
            case FunctionBean.FILE_RECOVERY:
                isHaveAuths(MemberType.APP_FILE_RECOVER, isTrial -> FileRecoverActivity.start(ActivityUtil.getTopActivity(), isTrial));
                break;
            case FunctionBean.IMG_CLEARING:
                ImageRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_IMAGE_CLEAN);
                break;
            case FunctionBean.VIDEO_RECOVERY:
                isHaveAuths(MemberType.APP_VIDEO_RECOVER, isTrial -> VideoRecoverActivity.start(ActivityUtil.getTopActivity(), isTrial));
                break;
        }
    }

    public interface NextStepCallback {
        void onNextStep(boolean isTrial);
    }


}
