package com.fusi24.entryPermitScanner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;

import com.fusi24.entryPermitScanner.base.BaseActivity;
import com.fusi24.entryPermitScanner.data.SessionManager;
import com.fusi24.entryPermitScanner.ui.home.HomeActivity;
import com.fusi24.entryPermitScanner.ui.login.LoginActivity;

public class SplashScreenActivity extends BaseActivity {

    public static final int REQUEST_LOGIN = 1100;

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

    @Override
    protected void onResume() {
        super.onResume();

        new Handler().postDelayed(() -> {
            SessionManager sessionManager = getSessionManager();
            if (!sessionManager.isUserLogin()) {
                Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
                startActivityForResult(intent, REQUEST_LOGIN);
            }else {
            goToHome();
        }
        },2000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        if (requestCode == REQUEST_LOGIN) {
            goToHome();
        }
    }

    private void goToHome() {
        Intent intent = new Intent(SplashScreenActivity.this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
}
