package com.ardraw.ardrawingflow.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ardraw.ardrawingflow.R;
import com.ardraw.ardrawingflow.databinding.ActivitySplashBinding;
import com.ardraw.ardrawingflow.max.MaxManager;
import com.ardraw.ardrawingflow.max.WelComManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private static final long TOTAL_TIME = 11000;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        MaxManager.onLoadAd();

        Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .transform(new RoundedCorners(16))
                .into(binding.splashImage);

//        countDownTimer = new CountDownTimer(TOTAL_TIME, 100) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//                int percentage = (int) (100 - (float) millisUntilFinished / TOTAL_TIME * 100);
//                binding.progressBar.setProgress(percentage);
//            }
//
//            @Override
//            public void onFinish() {
//                startMain();
//            }
//        };

        countDownTimer = WelComManager.initTimer(this, TOTAL_TIME, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                int percentage = (int) (100 - (float) aLong / TOTAL_TIME * 100);
                binding.progressBar.setProgress(percentage);
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                startMain();
                return null;
            }
        });

        countDownTimer.start();
    }

    private void startMain() {
        binding.progressBar.setProgress(100);

        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        binding = null;
    }
}