package com.lc.myapplication.security.locks.activity;

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

import androidx.appcompat.app.AppCompatActivity;

import com.lc.myapplication.security.locks.max.WelComManager;
import com.lc.myapplication.security.locks.Securitylocks;
import com.lc.myapplication.security.locks.max.CustomProgressBar;
import com.lc.myapplication.security.locks.tool.LockAction;
import com.lc.myapplication.security.locks.tool.LockPremission;
import com.lc.myapplication.security.locks.tool.MyTools;
import com.lc.myapplication.security.locks.R;

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

public class IntoActivity extends AppCompatActivity {
    private CountDownTimer timer;
    private CustomProgressBar progressBar;
    private Long time = 11000L;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_into);
        progressBar = findViewById(R.id.pb);
        timer = WelComManager.initTimer(IntoActivity.this, time, new Function1<Long, Unit>() {
            @Override
            public Unit invoke(Long aLong) {
                long number = time - aLong;
                Double s = calculatePercentage(number, time);
                Log.d(Securitylocks.TAG, "--------------l=" + s);
                progressBar.setProgress(s.floatValue());
                return null;
            }
        }, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                progressBar.setProgress(100);
                startSwitchActivity();
                return null;
            }
        });
    }

    private void startSwitchActivity() {
        String firstkey = MyTools.getPassword();
        Log.d("dhn","dhn------fin"+firstkey);
        if (firstkey.isEmpty()) {
            startActivity(new Intent(IntoActivity.this, PassCodeActivity.class));
            finish();
        } else {
            if(LockPremission.privacysuccess(this) && LockPremission.showonsuccess(this)){
                LockAction lockAction = LockAction.getActionInstance(this);
                MyTools.onRunMain(() -> {
                    lockAction.showLockView();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(IntoActivity.this, MainActivity.class));
                            finish();
                        }
                    });
                });
            }else {
                startActivity(new Intent(IntoActivity.this, MainActivity.class));
                finish();
            }
        }
    }
    private Double calculatePercentage(long number, long total) {
        if (total == 0) {
            return 0.00; // 防止除以0错误
        }
        return ((double) number / total) * 100;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}