package com.datarecovery.master.module.homepage;

import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.atmob.common.data.KVUtils;
import com.atmob.common.runtime.ActivityUtil;
import com.atmob.common.runtime.ContextUtil;
import com.atmob.mediation.api.AdError;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.AdFuncId;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.audiorecover.AudioRecoverActivity;
import com.datarecovery.master.module.backup_helper.BackupHelperActivity;
import com.datarecovery.master.module.filerecover.FileRecoverActivity;
import com.datarecovery.master.module.imgrecover.ImageRecoverActivity;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.module.videorecover.VideoRecoverActivity;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.sdk.ad.RewardVideoListenerAdapter;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.utils.Maps;
import com.datarecovery.master.utils.ToastUtil;

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

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;


@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 MutableLiveData<Boolean> isQQSelected = new MutableLiveData<>(false);

    @Inject
    public HomePageViewModel() {
        initList();
    }

    public LiveData<Boolean> getIsQQSelected() {
        return isQQSelected;
    }

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

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

    private void initList() {
        informationList.add(new Pair<>("华为用户 使用了微信消息备份", ""));
        informationList.add(new Pair<>("小米用户 使用了图片备份", ""));
        informationList.add(new Pair<>("OPPO用户 使用了微信好友备份", ""));
        informationList.add(new Pair<>("VIVO用户 使用了微信消息备份", ""));
        informationList.add(new Pair<>("荣耀用户 使用了音频备份", ""));
        informationList.add(new Pair<>("iPhone用户 使用了视频备份", ""));

        functionList.add(new FunctionBean(FunctionBean.BACKUP_HELPER,
                ContextUtil.getContext().getString(R.string.home_page_backup_helper),
                ContextUtil.getContext().getString(R.string.home_page_backup_helper_desc),
                R.drawable.icon_home_page_helper,
                R.drawable.bg_home_page_helper));
        functionList.add(new FunctionBean(FunctionBean.IMG_RECOVERY,
                ContextUtil.getContext().getString(R.string.home_page_img_recovery),
                ContextUtil.getContext().getString(R.string.home_page_img_recovery_desc),
                R.drawable.icon_home_page_img_recover,
                R.drawable.bg_home_page_img_recover));
        functionList.add(new FunctionBean(FunctionBean.FILE_RECOVERY,
                ContextUtil.getContext().getString(R.string.home_page_file_recovery),
                ContextUtil.getContext().getString(R.string.home_page_file_recovery_desc),
                R.drawable.icon_home_page_file_recovery,
                R.drawable.bg_home_page_file_recovery));
        functionList.add(new FunctionBean(FunctionBean.VIDEO_RECOVERY,
                ContextUtil.getContext().getString(R.string.home_page_video_recovery),
                ContextUtil.getContext().getString(R.string.home_page_video_recovery_desc),
                R.drawable.icon_home_page_video_recovery,
                R.drawable.bg_home_page_video_recovery));
        functionList.add(new FunctionBean(FunctionBean.AUDIO_RECOVERY,
                ContextUtil.getContext().getString(R.string.home_page_audio_recovery),
                ContextUtil.getContext().getString(R.string.home_page_audio_recovery_desc),
                R.drawable.icon_home_page_audio_recovery,
                R.drawable.bg_home_page_audio_recovery));
        functionList.add(new FunctionBean(FunctionBean.IMG_CLEARING,
                ContextUtil.getContext().getString(R.string.home_page_img_clearing),
                ContextUtil.getContext().getString(R.string.home_page_img_clearing_desc),
                R.drawable.icon_home_page_img_clearing,
                R.drawable.bg_home_page_img_clearing));
    }

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

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

    public void toggleAreaSwitch() {
        if (BoxingUtil.boxing(isQQSelected.getValue())) {
            onWeChatAreaClick();
        } else {
            onQQAreaClick();
        }
    }

    public void onQQAreaClick() {
        isQQSelected.setValue(true);

        AtmobAdHelper.showInterstitial(AdFuncId.INTERSTITIAL_HOME_SWITCH, null);

        EventHelper.report(EventId.EVENT_3000404, Maps.asMap("zone_id", 102));
    }

    public void onWeChatAreaClick() {
        isQQSelected.setValue(false);

        AtmobAdHelper.showInterstitial(AdFuncId.INTERSTITIAL_HOME_SWITCH, null);

        EventHelper.report(EventId.EVENT_3000404, Maps.asMap("zone_id", 101));
    }

    public void onBannerClick() {
        ImageRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_IMAGE_RECOVER);

        EventHelper.report(EventId.EVENT_3000405);
    }

    public void onBackupHelperClick() {
        EventHelper.report(EventId.EVENT_3001000);
        AtmobAdHelper.showVideo(AdFuncId.REWARD_BACKUP_TUTORIAL, new RewardVideoListenerAdapter() {
            boolean isRewarded = false;

            @Override
            public void onRewarded(@NonNull String token) {
                super.onRewarded(token);
                isRewarded = true;
                BackupHelperActivity.start(ActivityUtil.getTopActivity(), isQQSelected.getValue() == null || !isQQSelected.getValue());
            }

            @Override
            public void onRewardAdShowFail(AdError adError) {
                super.onRewardAdShowFail(adError);
                BackupHelperActivity.start(ActivityUtil.getTopActivity(), isQQSelected.getValue() == null || !isQQSelected.getValue());
            }

            @Override
            public void onLoadFailed(AdError adError) {
                super.onLoadFailed(adError);
                BackupHelperActivity.start(ActivityUtil.getTopActivity(), isQQSelected.getValue() == null || !isQQSelected.getValue());
            }

            @Override
            public void onRewardAdClosed() {
                super.onRewardAdClosed();
                if (!isRewarded) {
                    ToastUtil.show("解锁失败", ToastUtil.LENGTH_SHORT);
                }
            }
        });
    }

    public void clickItemFunction(FunctionBean bean) {
        if (bean == null) {
            return;
        }
        boolean isQQ = isQQSelected.getValue() != null && isQQSelected.getValue();
        boolean isFirstByApp;
        if (isQQ) {
            boolean isFirstClickQq = KVUtils.getDefault().getBoolean("is_first_click_qq", true);
            isFirstByApp = isFirstClickQq;
            if (isFirstClickQq) {
                KVUtils.getDefault().putBoolean("is_first_click_qq", false);
            }
        } else {
            boolean isFirstClickWeChat = KVUtils.getDefault().getBoolean("is_first_click_wechat", true);
            isFirstByApp = isFirstClickWeChat;
            if (isFirstClickWeChat) {
                KVUtils.getDefault().putBoolean("is_first_click_wechat", false);
            }
        }
        boolean isFirst = KVUtils.getDefault().getBoolean("is_first_click_function", true);
        if (isFirst) {
            KVUtils.getDefault().putBoolean("is_first_click_function", false);
        }
        switch (bean.getFunctionId()) {
            case FunctionBean.AUDIO_RECOVERY:
                AudioRecoverActivity.start(ActivityUtil.getTopActivity());
                EventHelper.report(EventId.EVENT_3000005, Maps.asMap("id", 104));
                if (isQQ) {
                    EventHelper.report(EventId.EVENT_3000402, com.atmob.analytics.utils.Maps.of(
                            "qq_id", 104,
                            "is_first_click_qq", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 109));
                    }
                } else {
                    EventHelper.report(EventId.EVENT_3000401, com.atmob.analytics.utils.Maps.of(
                            "weixin_id", 104,
                            "is_first_click_weixin", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 104));
                    }
                }
                break;
            case FunctionBean.FILE_RECOVERY:
                FileRecoverActivity.start(ActivityUtil.getTopActivity());
                EventHelper.report(EventId.EVENT_3000005, Maps.asMap("id", 102));
                if (isQQ) {
                    EventHelper.report(EventId.EVENT_3000402, com.atmob.analytics.utils.Maps.of(
                            "qq_id", 102,
                            "is_first_click_qq", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 107));
                    }
                } else {
                    EventHelper.report(EventId.EVENT_3000401, com.atmob.analytics.utils.Maps.of(
                            "weixin_id", 102,
                            "is_first_click_weixin", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 102));
                    }
                }
                break;
            case FunctionBean.IMG_CLEARING:
                ImageRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_IMAGE_CLEAN);
                if (isQQ) {
                    EventHelper.report(EventId.EVENT_3000402, com.atmob.analytics.utils.Maps.of(
                            "qq_id", 105,
                            "is_first_click_qq", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 110));
                    }
                } else {
                    EventHelper.report(EventId.EVENT_3000401, com.atmob.analytics.utils.Maps.of(
                            "weixin_id", 105,
                            "is_first_click_weixin", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 105));
                    }
                }
                break;
            case FunctionBean.VIDEO_RECOVERY:
                VideoRecoverActivity.start(ActivityUtil.getTopActivity());
                EventHelper.report(EventId.EVENT_3000005, Maps.asMap("id", 103));
                if (isQQ) {
                    EventHelper.report(EventId.EVENT_3000402, com.atmob.analytics.utils.Maps.of(
                            "qq_id", 103,
                            "is_first_click_qq", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 108));
                    }
                } else {
                    EventHelper.report(EventId.EVENT_3000401, com.atmob.analytics.utils.Maps.of(
                            "weixin_id", 103,
                            "is_first_click_weixin", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 103));
                    }
                }
                break;
            case FunctionBean.IMG_RECOVERY:
                ImageRecoverActivity.start(ActivityUtil.getTopActivity(), MemberType.APP_IMAGE_RECOVER);
                EventHelper.report(EventId.EVENT_3000005, Maps.asMap("id", 101));
                if (isQQ) {
                    EventHelper.report(EventId.EVENT_3000402, com.atmob.analytics.utils.Maps.of(
                            "qq_id", 101,
                            "is_first_click_qq", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 106));
                    }
                } else {
                    EventHelper.report(EventId.EVENT_3000401, com.atmob.analytics.utils.Maps.of(
                            "weixin_id", 101,
                            "is_first_click_weixin", isFirstByApp
                    ));
                    if (isFirst) {
                        EventHelper.report(EventId.EVENT_3000403, Maps.asMap("function_id", 101));
                    }
                }
                break;
            case FunctionBean.BACKUP_HELPER:
                onBackupHelperClick();
                break;
        }
    }
}
