package com.younki.maidmatching.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import com.younki.maidmatching.services.SharedPreference.Preferences;
import com.younki.maidmatching.ui.auth.LoginActivity;
import com.younki.maidmatching.ui.maid.MasterMaidActivity;
import com.younki.maidmatching.ui.owner.MasterOwnerActivity;

import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {
    BroadcastReceiver broadcastReceiver = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    public void checkInternetStatus(Context context) {
        CheckInternet.showNoInternetDialog(context);
        broadcastReceiver = new CheckInternet();
        internetStatus();
    }

    public void internetStatus(){
        registerReceiver(broadcastReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    public String getEditTextValue(EditText editText){
        try {
            return editText.getText().toString();
        } catch (Exception e){
            return "";
        }
    }

    public void checkPreferencesAuth(boolean finish, Context context){
        if (Preferences.getStatus(getBaseContext())) {
            if (finish) {
                finish();
            } else {
                if (Preferences.getRole(context) == 2) {
                    Intent intent = new Intent(context, MasterOwnerActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(context, MasterMaidActivity.class);
                    startActivity(intent);
                }
            }
        }
    }

    public void checkPreferencesApp(Context context){
        if (!Preferences.getStatus(getBaseContext())) {
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }

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