package com.application.tak.takapplication;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.data_access.GetUserById;
import com.application.tak.takapplication.data_model.User;
import com.application.tak.takapplication.interfaces.OnDBRequestFinished;
import com.application.tak.takapplication.local_db.LocalDB;
import com.application.tak.takapplication.services.LoginStatus;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class actWelcomeScrean extends AppCompatActivity {

    private static final String TAG = "Welcome Hash";
    public EditText welcome;
    Context ctx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_welcome_screan);

        ctx = getApplicationContext();
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

        AutoLogIn();


    }


    void AutoLogIn()
    {
        LoginStatus lgStatus = new LoginStatus(ctx);

        User u = lgStatus.IsLoggedIn();

        if(u!= null)
        {   GetUserById getUsr;
            if(u.get_RoleId() == 2)
            {
                getUsr = new GetUserById("Client",u.get_Id(),ctx, false);
            }
            else if(u.get_RoleId() == 1)
            {
                getUsr = new GetUserById("Student",u.get_Id(),ctx, false);
            }
            else{
                getUsr = new GetUserById("",-1,ctx, false);
            }

            if(getUsr != null)
            {
                getUsr.setDBRequestFinishedListener(new OnDBRequestFinished() {
                    @Override
                    public void onDBRequestFinished() {
                        if(getUsr._client!= null)
                        {
                            Config.LoggedInClient = getUsr._client;
                            Intent i = new Intent(ctx,actClientTask.class);
                            startActivity(i);
                            finish();
                        }
                        else if(getUsr._student != null)
                        {
                            Config.LoggedInStudent = getUsr._student;
                            Intent i = new Intent(ctx,actStudentTask.class);
                            startActivity(i);
                            finish();
                        }
                        else if(getUsr.dbResult != null)
                        {
                            Intent i = new Intent(ctx,actFirstUsage.class);
                            startActivity(i);
                            finish();
                        }
                    }
                });

            }
            else{
                Intent i = new Intent(ctx,actFirstUsage.class);
                startActivity(i);
                finish();
            }
        }
        else{
            Intent i = new Intent(ctx,actFirstUsage.class);
            startActivity(i);
            finish();
        }
    }

}
