package com.hw.nativeapp.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hw.nativeapp.R;

public class SplashActivity extends Activity implements View.OnClickListener {


    public static final Integer COUNTDOWN_TIME = 2000;

    private TextView txJump;
    private TextView txContentdown;
    private CountDownTimer timer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        txJump = findViewById(R.id.tx_jump);
        txContentdown = findViewById(R.id.tx_contentdown);
        txJump.setOnClickListener(this);

        startCountDownTimer();
//        jumpToLogin();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tx_jump) {
            jumpToLogin();
        }
    }

    private void jumpToLogin(){
        stopCountDownTimer();

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

    private void stopCountDownTimer(){
        if (timer != null){
            timer.cancel();
        }
    }

    private void startCountDownTimer(){
        timer = new CountDownTimer(COUNTDOWN_TIME, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                txContentdown.setText("倒计时" + millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                jumpToLogin();
            }
        };
        timer.start();
    }
}
