package code.abdulrahman.bookStore;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.snackbar.Snackbar;

public abstract class BaseActivity extends AppCompatActivity {
    Snackbar snackbar;
    View testView;
    String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Toolbar myToolbar = findViewById(R.id.in_base_my_toolbar);
        setSupportActionBar(myToolbar);

        snackbar = Snackbar.make(myToolbar, message, Snackbar.LENGTH_LONG)
                .setAction("Action", null);

        onViewReady(savedInstanceState, getIntent());
    }

    protected void onViewReady(Bundle savedInstanceState, Intent intent) {
        // To be used by child activities.
    }

    protected abstract int getContentView();

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {
        // invoked automatically by activity
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
            case R.id.action_one:
                Intent intent = new Intent(BaseActivity.this, ChildActivity.class);
                startActivity(intent);
                break;
            case R.id.action_two:
                snackbar.setText("You are at home").show();
                break;

            case R.id.action_three:
                Uri webpage = Uri.parse("http://www.wlu.ca/faculty/abdul-rahman-mawlood-yunis");
                intent = new Intent(Intent.ACTION_VIEW, webpage);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
                break;

            case R.id.action_about:
                Toast.makeText(this,
                        "Version 1.0," + " Abdul-Rahman Mawlood-Yunis",
                        Toast.LENGTH_LONG).show();
        }
        return true;
    }
}
