package com.atmob.voiceai.module.splash;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AnimationSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.common.ui.SizeUtil;
import com.atmob.voiceai.R;
import com.atmob.voiceai.data.consts.Constants;
import com.atmob.voiceai.databinding.ActivitySplashBinding;
import com.atmob.voiceai.module.main.MainActivity;
import com.atmob.voiceai.utils.CommonUtils;
import com.atmob.voiceai.utils.SpannableUtil;

import dagger.hilt.android.AndroidEntryPoint;

@SuppressLint("CustomSplashScreen")
@AndroidEntryPoint
public class SplashActivity extends BaseActivity<ActivitySplashBinding> {


    private SplashViewModel splashViewModel;

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


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

    private void initView() {

    }

    private void initObserver() {
        splashViewModel.setCanNext(true);
        splashViewModel.getShowMainEvent().observe(this, o -> showMain());
        splashViewModel.getCurrentGuide().observe(this, current -> {
            if (current == null) {
                return;
            }
            if (SplashViewModel.GUIDE_PAGE_1 == current) {
                startGuideEnter(binding.ivGuideHeader1, binding.tvGuideHeader1, binding.ivGuide1, 0, 400);
            } else if (SplashViewModel.GUIDE_PAGE_2 == current) {
                startGuideLeave(binding.ivGuideHeader1, binding.tvGuideHeader1, binding.ivGuide1);
                startGuideEnter(binding.ivGuideHeader2, binding.tvGuideHeader2, binding.ivGuide2, 600, 0);
            } else if (SplashViewModel.GUIDE_PAGE_3 == current) {
                startGuideLeave(binding.ivGuideHeader2, binding.tvGuideHeader2, binding.ivGuide2);
                new Handler().postDelayed(() -> binding.tvNextStep.animate().alpha(0).setDuration(200).start(), 400);
                showEndGuide();
            } else if (SplashViewModel.GUIDE_PAGE_END == current) {
                closeGuide();
            }
        });
    }

    private void closeGuide() {
        goMain();
    }

    private void showEndGuide() {
        binding.tvPracticalFunny.setVisibility(View.VISIBLE);
        binding.tvPractical.setVisibility(View.VISIBLE);
        binding.tvGuideDone.setVisibility(View.VISIBLE);
        binding.tvPrivacyPolicy.setVisibility(View.VISIBLE);
        String allTxt = getString(R.string.splash_privacy_all_txt);
        String targetTxt1 = getString(R.string.splash_privacy_policy);
        String targetTxt2 = getString(R.string.splash_terms_of_service);
        SpannableUtil.getAgreementSpannableStringBuilder(binding.tvPrivacyPolicy, allTxt, new String[]{targetTxt1, targetTxt2}, getResources().getColor(R.color.colorPrimaryVariant), false,
                v -> {
                    CommonUtils.startWeb(Constants.PRIVACY_POLICY);
                },
                v -> {
                    CommonUtils.startWeb(Constants.USER_AGREEMENT);
                });
        ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0f, 1f);
        alphaAnimator.setStartDelay(400);
        alphaAnimator.setDuration(800);
        new Handler().postDelayed(() -> binding.lottieGuide.playAnimation(), 400);
        alphaAnimator.addUpdateListener(animation -> {
            float alpha = (float) animation.getAnimatedValue();
            binding.lottieBox.setAlpha(alpha);
            binding.tvPracticalFunny.setAlpha(alpha);
            binding.tvPractical.setAlpha(alpha);
            binding.tvGuideDone.setAlpha(alpha);
            binding.tvPrivacyPolicy.setAlpha(alpha);
        });
        alphaAnimator.start();
    }


    private void startGuideEnter(@NonNull View headerView1, @NonNull View headerView2, @NonNull View contentView, int startDelay, int contentStartDelay) {
        ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0f, 1f);
        alphaAnimator.setDuration(800);
        alphaAnimator.setStartDelay(startDelay);
        alphaAnimator.addUpdateListener(animation -> {
            float alpha = (float) animation.getAnimatedValue();
            headerView1.setAlpha(alpha);
            headerView2.setAlpha(alpha);
        });

        ValueAnimator contentAnimator = ValueAnimator.ofFloat(SizeUtil.getScreenWidth(), 0f);
        contentAnimator.setStartDelay(startDelay + contentStartDelay);
        contentAnimator.setDuration(800);
        contentAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            contentView.setX(value);
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(alphaAnimator, contentAnimator);
        animatorSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                splashViewModel.setCanNext(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                splashViewModel.setCanNext(true);
            }
        });
        animatorSet.start();
    }


    private void startGuideLeave(@NonNull View headerView1, @NonNull View headerView2, @NonNull View contentView) {
        ValueAnimator alphaAnimator = ValueAnimator.ofFloat(1f, 0f);
        alphaAnimator.setDuration(600);
        alphaAnimator.addUpdateListener(animation -> {
            float alpha = (float) animation.getAnimatedValue();
            headerView1.setAlpha(alpha);
            headerView2.setAlpha(alpha);
        });
        alphaAnimator.start();

        ValueAnimator contentAnimator = ValueAnimator.ofFloat(0f, SizeUtil.getScreenWidth());
        contentAnimator.setDuration(800);
        contentAnimator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            contentView.setX(-value);
        });
        contentAnimator.start();
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        splashViewModel = getViewModelProvider().get(SplashViewModel.class);
        binding.setSplashViewModel(splashViewModel);
    }


    private void showMain() {
        new Handler().postDelayed(this::goMain, 2000);
    }

    private void goMain() {
        MainActivity.start(this);
        finish();
    }


}
