package com.datarecovery.master.data.repositories;


import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.atmob.common.logging.AtmobLog;
import com.atmob.common.runtime.ContextUtil;
import com.atmob.sdk.gravity_engine.GravityEngine;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.data.api.AtmobApi;
import com.datarecovery.master.data.api.request.CustomerUrlRequest;
import com.datarecovery.master.data.api.response.CustomerUrlResponse;
import com.datarecovery.master.data.consts.ChannelId;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.utils.RxHttpHandler;

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

import javax.inject.Inject;
import javax.inject.Singleton;

import atmob.reactivex.rxjava3.core.Single;
import atmob.rxjava.utils.RxJavaUtil;
import dagger.hilt.EntryPoint;
import dagger.hilt.InstallIn;
import dagger.hilt.android.EntryPointAccessors;
import dagger.hilt.components.SingletonComponent;

@Singleton
public class ConfigRepository {


    private static final String[] trialMember = new String[]{
            MemberType.APP_IMAGE_RECOVER,
            MemberType.APP_FILE_RECOVER,
            MemberType.APP_VIDEO_RECOVER,
            MemberType.APP_AUDIO_RECOVER,
    };

    private final MutableLiveData<Boolean> isOpenTrialMembership = new MutableLiveData<>();
    private final List<String> trialMemberList = new ArrayList<>(Arrays.asList(trialMember));
    private final AtmobApi atmobApi;

    @Inject
    public ConfigRepository(AtmobApi atmobApi) {
        this.atmobApi = atmobApi;
        getTrialMembershipStatus();
    }

    public Single<CustomerUrlResponse> getCustomerUrl(String userId, String phone) {
        return atmobApi.getCustomerUrl(new CustomerUrlRequest(userId, phone))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }


    private void getTrialMembershipStatus() {
        //如果是商店包，都开启试用会员
        if (AtmobUser.getAtmobTgPlatformId() == ChannelId.SD) {
            isOpenTrialMembership.setValue(true);
            return;
        }
        GravityEngine.addAttributeCallback(attributed -> {
            AtmobLog.d("ConfigRepository", "registerAttributionResultCallback attributed: " + attributed);
            if (BoxingUtil.boxing(attributed)) {
                //归因成功
                isOpenTrialMembership.setValue(false);
                return;
            }
            if (AtmobUser.getAtmobTgPlatformId() != ChannelId.SD || TextUtils.isEmpty(AtmobUser.getAtmobChannel())) {
                isOpenTrialMembership.setValue(false);
                return;
            }
            isOpenTrialMembership.setValue(true);
        });
    }

    public LiveData<Boolean> getIsOpenTrialMembership() {
        return isOpenTrialMembership;
    }

    public boolean isIsOpenTrialMembership() {
        return BoxingUtil.boxing(isOpenTrialMembership.getValue());
    }

    public boolean isConformTrialAuths(@MemberType String auth) {
        if (!BoxingUtil.boxing(isOpenTrialMembership.getValue())) {
            return false;
        }
        return isHaveTrialAuth(auth);
    }

    private boolean isHaveTrialAuth(@MemberType String auth) {
        if (TextUtils.isEmpty(auth)) {
            return false;
        }
        return trialMemberList.contains(auth);
    }

    public static ConfigRepository getInstance() {
        ConfigRepositoryPoint configRepositoryPoint = EntryPointAccessors.fromApplication(ContextUtil.getContext(), ConfigRepositoryPoint.class);
        return configRepositoryPoint.configRepository();
    }

    @InstallIn(SingletonComponent.class)
    @EntryPoint
    interface ConfigRepositoryPoint {
        ConfigRepository configRepository();
    }
}
