package com.atmob.voiceai.module.setting;

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

import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.voiceai.databinding.SettingActivityBinding;
import com.atmob.voiceai.dialog.ScoringDialog;
import com.atmob.voiceai.handlers.EventHandler;
import com.atmob.voiceai.sdk.kochava.KochavaHelper;
import com.atmob.voiceai.utils.ToastUtil;

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 SettingActivity extends BaseActivity<SettingActivityBinding> {

    private SettingViewModel settingViewModel;

    private int eventTimes = 0;
    private Disposable eventSubscribe;

    private int attrTimes = 0;
    private Disposable attrSubscribe;

    private ScoringDialog scoringDialog;

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


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

    private void initObserver() {
        settingViewModel.getShowScoringEvent().observe(this, o -> showScoringDialog());
    }

    private void initView() {
        addTopStatusBarHeight(binding.toolbar);
        binding.toolbar.setNavigationOnClickListener(v -> finish());

        binding.vEvent.setOnClickListener(v -> {
            if (eventTimes == 0) {
                if (eventSubscribe != null) {
                    eventSubscribe.dispose();
                }
                eventSubscribe = Observable.timer(5000, TimeUnit.MILLISECONDS)
                        .subscribe(aLong -> eventTimes = 0);
                eventTimes++;
            } else if (eventTimes == 10) {
                showEventInfo();
            } else {
                eventTimes++;
            }
        });

        binding.vAttr.setOnClickListener(v -> {
            if (attrTimes == 0) {
                if (attrSubscribe != null) {
                    attrSubscribe.dispose();
                }
                attrSubscribe = Observable.timer(5000, TimeUnit.MILLISECONDS)
                        .subscribe(aLong -> attrTimes = 0);
                attrTimes++;
            } else if (attrTimes == 10) {
                showAttrInfo();
            } else {
                attrTimes++;
            }
        });
    }

    private void showScoringDialog() {
        if (scoringDialog == null) {
            scoringDialog = new ScoringDialog(this);
            scoringDialog.setScoringDialogListener(() -> settingViewModel.setHideScoring());
        }
        scoringDialog.show();
    }

    private void showAttrInfo() {
        StringBuffer sb = new StringBuffer();
        sb.append("ka attribute:");
        sb.append(KochavaHelper.isAttributed());
        ToastUtil.show(sb.toString(), ToastUtil.LENGTH_SHORT);
    }

    private void showEventInfo() {
        EventHandler.setToastReport();
        ToastUtil.show("Event toast is open.", ToastUtil.LENGTH_SHORT);
    }


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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        settingViewModel = getViewModelProvider().get(SettingViewModel.class);
        binding.setSettingViewModel(settingViewModel);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (eventSubscribe != null) {
            eventSubscribe.dispose();
        }
        if (attrSubscribe != null) {
            attrSubscribe.dispose();
        }
    }
}
