package com.datarecovery.master.data.repositories;


import android.text.TextUtils;

import com.atmob.common.logging.AtmobLog;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.BuildConfig;
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.sdk.gravity.GravityHelper;
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 java.util.Objects;

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

import atmob.reactivex.rxjava3.core.Single;
import atmob.rxjava.utils.RxJavaUtil;

@Singleton
public class ConfigRepository {

    private static boolean isOpenTrialMembership = false;

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

    private static 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 = true;
            return;
        }
        GravityHelper.registerAttributionResultCallback(attributed -> {
            AtmobLog.d("ConfigRepository", "registerAttributionResultCallback attributed: " + attributed);
            if (BoxingUtil.boxing(attributed)) {
                //归因成功
                isOpenTrialMembership = false;
                return;
            }
            if (AtmobUser.getAtmobTgPlatformId() != ChannelId.SD || TextUtils.isEmpty(AtmobUser.getAtmobChannel())) {
                isOpenTrialMembership = false;
                return;
            }
            isOpenTrialMembership = true;
        });
    }


    public static boolean isTargetSuffixChannel(String targetChannel) {
        String atmobChannel = AtmobUser.getAtmobChannel();
        if (atmobChannel == null || targetChannel == null) {
            return false;
        }
        int index = atmobChannel.indexOf(targetChannel, atmobChannel.length() - targetChannel.length());
        return index != -1;
    }

    public static boolean isIsOpenTrialMembership() {
        return isOpenTrialMembership;
    }

    public static boolean isConformTrialAuths(@MemberType String auth) {
        if (!isOpenTrialMembership) {
            return false;
        }
        return isHaveTrialAuth(auth);
    }

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