package com.wall.various.wallpaper.ui;

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

import androidx.appcompat.app.AppCompatActivity;

import com.wall.various.wallpaper.ad.AdManager;
import com.wall.various.wallpaper.ad.CountAction;
import com.wall.various.wallpaper.ad.GoMainAction;
import com.wall.various.wallpaper.databinding.UiWelcomeBinding;
import com.wall.various.wallpaper.helper.Common;

public class UiWelcome extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private UiWelcomeBinding vb;

    private long totalTime = 12000;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = UiWelcomeBinding.inflate(getLayoutInflater());
        Common.initFull(this,false);
        setContentView(vb.getRoot());


//        countDownTimer = new CountDownTimer(totalTime,100) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//                int progressPercentage = (int) ((100 * millisUntilFinished) / totalTime);
//
//                int countdownPercentage = 100 - progressPercentage;
//
//                vb.progressbar.setProgress(countdownPercentage);
//            }
//
//            @Override
//            public void onFinish() {
//                vb.progressbar.setProgress(100);
//                Intent intent = new Intent(UiWelcome.this, UiMain.class);
//                startActivity(intent);
//                finish();
//            }
//        };

        countDownTimer = AdManager.showWelcomeAd(this, totalTime, new CountAction() {
            @Override
            public void onCount(long millisUntilFinished) {
                int progressPercentage = (int) ((100 * millisUntilFinished) / totalTime);

                int countdownPercentage = 100 - progressPercentage;

                vb.progressbar.setProgress(countdownPercentage);
            }
        }, new GoMainAction() {
            @Override
            public void onGo() {
                vb.progressbar.setProgress(100);
                Intent intent = new Intent(UiWelcome.this, UiMain.class);
                startActivity(intent);
                finish();
            }
        });
        countDownTimer.start();

    }



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