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.repositories.DeviceFuncRepository;
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 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<>("华为P40用户  微信消息恢复", "2分钟前"));
        informationList.add(new Pair<>("小米Mi 11用户 图片恢复", "4分钟前"));
        informationList.add(new Pair<>("三星S20用户 微信好友恢复", "5分钟前"));
        informationList.add(new Pair<>("OPPOFind X3用户 微信消息恢复", "9分钟前"));
        informationList.add(new Pair<>("VivoY72用户 音频恢复", "11分钟前"));
        informationList.add(new Pair<>("华为P30用户 视频恢复", "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, () -> WeChatRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_WX_MESSAGE_RECOVER));
    }

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

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

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


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

    }

    public interface NextStepCallback {
        void onNextStep();
    }



}
