package w.soulofpluto.posstock;

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.FirebaseApp;


 /*
  Create by Ramesh Babu
 */

public class SplashActivity extends AppCompatActivity{
    PreferenceManager preferenceManager;
    BluetoothAdapter bluetoothAdapter;
    private int REQUEST_ENABLE_BT = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        setContentView(R.layout.activity_splash);
        preferenceManager = new PreferenceManager(this);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        FirebaseApp.initializeApp(this);
        //FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);

        //FirebaseApp.getInstance().setAutomaticResourceManagementEnabled(true);
        //FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);


        //FirebaseApp.getInstance().
        if(bluetoothAdapter!=null && bluetoothAdapter.isEnabled()){
            runOnUiThread(() -> splashMethod());
            //throw new RuntimeException("Test Crash");
        }else{
            //Constants.getToast(this,"Please Enable your bluetooth device");
            try {
                showDialog("Please Enable your bluetooth device");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onCreate(savedInstanceState);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == RESULT_OK) {
                Constants.getToast(this,"BlueTooth Turned On");
                runOnUiThread(new Runnable(){
                    @Override
                    public void run(){
                        splashMethod();
                    }
                });
                //Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show();
            } else {
                //Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
                Constants.getToast(this,"Cancelled");
            }
        }
    }
    public void showDialog(final String phone) throws Exception
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
        builder.setMessage(phone);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.show();
    }
    public void splashMethod(){
        Handler handler = new Handler();
        Runnable runnable = new Runnable(){
            @Override
            public void run(){
                //TODO Auto-generated method stub
                /*startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();*/
                if(preferenceManager.isLogged()){
                   startActivity(new Intent(SplashActivity.this, HomeActivity.class));
                   finish();
                }else{
                   startActivity(new Intent(SplashActivity.this, Login.class));
                   finish();
                }
            }
        };
        handler.postDelayed(runnable, 2500);
    }
}
