package cpm.com.gskmtorange;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.password.MPinActivity;

public class SplashScreenActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;
    LinearLayout linearLayout;
    private Context context;
    private SharedPreferences preferences = null;

   /* public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }*/

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        try {
            // Optional: hide the status bar safely
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            );
        }catch (Exception ignored){}
        setContentView(R.layout.activity_splash_main_layout);
        linearLayout = findViewById(R.id.lin_lay);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        startSplashTimer();
    }


    private void sendToLogin() {
        try {
            String mpin = preferences.getString(CommonString.MPIN, null);
            Intent intent;
            if (mpin != null) {
                intent = new Intent(this, MPinActivity.class);
                intent.putExtra(CommonString.IS_PASSWORD_CHECK, true);
            } else {
                intent = new Intent(this, LoginActivity.class);
            }
            startActivity(intent);
            finish();
        } catch (Exception ignored) {} finally {
            String mpin = preferences.getString(CommonString.MPIN, null);
            Intent intent;
            if (mpin != null) {
                intent = new Intent(this, MPinActivity.class);
                intent.putExtra(CommonString.IS_PASSWORD_CHECK, true);
            } else {
                intent = new Intent(this, LoginActivity.class);
            }

            startActivity(intent);
            finish();
        }

    }

    private void startSplashTimer() {
        new Handler(Looper.getMainLooper()).postDelayed(this::sendToLogin, 3000);
    }

}
