package com.snap.wall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.ad.tradpluslibrary.TPAdManager;
import com.snap.wall.App;
import com.snap.wall.R;
import com.snap.wall.mytool.Comutils;
import android.os.CountDownTimer;

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

public class OneActivity extends AppCompatActivity {
    private static final long TOTAL_TIME = 12000;
    // 进度更新间隔（50毫秒）
    private static final long UPDATE_INTERVAL = 50;

    private ProgressBar progressBar;
    private TextView tvProgress;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Comutils.initFull(this, false);
        setContentView(R.layout.activity_one);
        progressBar = findViewById(R.id.progressbar);
        tvProgress = findViewById(R.id.tv_progress);
        progressBar.setMax(100);
        startCountDown();
    }
    private void startCountDown() {
        TPAdManager.INSTANCE.init(
                this,
                App.TAG,
                "909D20753E5D8EB9FEC5DD675DF72811",
                "DDFEBCA71E78A60F5F089D3A5FBC0912",
                "1A841D5996F8106028429E8799174612",
                "BDEA3F104E55C2518696BD77AA80C112",
                new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        return null;
                    }
                }
        ) ;
        countDownTimer = TPAdManager.INSTANCE.showWelcomeAd(this, TOTAL_TIME, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                long elapsedTime = TOTAL_TIME - aLong;
                int progress = (int) (elapsedTime * 100 / TOTAL_TIME);
                progressBar.setProgress(progress);
                tvProgress.setText(progress + "%");
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                progressBar.setProgress(100);
                tvProgress.setText("100%");
                Intent intent = new Intent(OneActivity.this, PrimaryActivity.class);
                startActivity(intent);
                finish();
                return null;
            }
        }) ;
        countDownTimer.start();
    }

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