package com.datarecovery.master.module.about;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.central.AtmobCentral;
import com.atmob.sdk.gravity_engine.GravityEngine;
import com.atmob.user.AtmobUser;
import com.datarecovery.master.data.consts.AdFuncId;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.data.repositories.ConfigRepository;
import com.datarecovery.master.databinding.ActivityAboutBinding;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.browser.BrowserActivity;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.utils.SystemUtil;
import com.datarecovery.master.utils.ToastUtil;
import com.gyf.immersionbar.ImmersionBar;

import java.util.concurrent.TimeUnit;

import atmob.reactivex.rxjava3.core.Observable;
import atmob.reactivex.rxjava3.disposables.Disposable;
import dagger.hilt.android.AndroidEntryPoint;

/**
 * 关于
 */
@AndroidEntryPoint
public class AboutActivity extends BaseActivity<ActivityAboutBinding> {


    private int times = 0;
    private Disposable subscribe;

    public static void start(Context context) {
        Intent intent = new Intent(context, AboutActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected boolean shouldImmersion() {
        return true;
    }

    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        immersionBar.statusBarDarkFont(true);
    }

    private void initView() {
        binding.tvVersionCode.setText(SystemUtil.getVersionName(getBaseContext()));
        binding.toolBar.setNavigationOnClickListener(v -> onBackPressed());
        addTopStatusBarHeight(binding.toolBar);
        binding.setUserAgreementClick(v -> {
            BrowserActivity.start(this, Constants.USER_AGREEMENT);
            EventHelper.report(EventId.EVENT_3020200);
        });
        binding.setPrivacyAgreementClick(v -> {
            BrowserActivity.start(this, Constants.PRIVACY_POLICY);
            EventHelper.report(EventId.EVENT_3020201);
        });
        binding.vTrial.setOnClickListener(v -> {
            if (times == 0) {
                if (subscribe != null && !subscribe.isDisposed()) {
                    subscribe.dispose();
                }
                subscribe = Observable.timer(5000, TimeUnit.MILLISECONDS)
                        .subscribe(aLong -> times = 0);
                times++;
            } else if (times == 10) {
                showTrialToast();
            } else {
                times++;
            }
        });
        binding.setInformationListClick(v -> {
            BrowserActivity.start(this, Constants.USER_INFO_LIST);
            EventHelper.report(EventId.EVENT_3020202);
        });
        binding.setSdkSharingListClick(v -> {
            BrowserActivity.start(this, Constants.THREE_SDK_LIST);
            EventHelper.report(EventId.EVENT_3020203);
        });
        binding.setKipProtectionClick(v -> BrowserActivity.start(this, Constants.KID_PROTECTION));

        AtmobAdHelper.showNative(AdFuncId.NATIVE_MINE, binding.adContainer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscribe != null && !subscribe.isDisposed()) {
            subscribe.dispose();
        }
    }

    private void showTrialToast() {
        //显示 是否开启试用 -- 打包渠道 -- 归因结果 -- 归因渠道
        StringBuilder sb = new StringBuilder();
        sb.append("--");
        sb.append(ConfigRepository.getInstance().isIsOpenTrialMembership());

        sb.append("--");
        sb.append(AtmobUser.getAtmobChannel());

        sb.append("--");
        sb.append(GravityEngine.getAttributionStatus() == GravityEngine.ATTRIBUTED);

        sb.append("--");
        String gravityCompany = AtmobCentral.getTgPlatform() + "";
        sb.append((TextUtils.isEmpty(gravityCompany)) ? "null" : gravityCompany);

        ToastUtil.show(sb.toString(), ToastUtil.LENGTH_SHORT);
    }
}