package com.github.prawncake.biomapapp;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.couchbase.lite.Context;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Manager;
import com.couchbase.lite.android.AndroidContext;
import com.github.eternaldeiwos.biomapapp.LoginActivity;

import java.io.IOException;

import eternaldeiwos.github.com.biomapapp.R;

//This class acts as a base class to be included in any activity so that the menu items are all the same across all classes that inherit this.
public class BaseActivity extends AppCompatActivity
{
    private final static String DB_NAME = "biomap_couchbase";
    private Database database;
    private Manager manager;

    public Database getDatabaseInstance() throws CouchbaseLiteException, IOException {
        if (database == null) {
            database =  getManagerInstance().getDatabase(DB_NAME);
        }
        return database;
    }

    public Manager getManagerInstance() throws IOException {
        if (manager == null) {
            manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
        }
        return manager;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent intent;
        switch (item.getItemId())
        {
            case R.id.settings_menu:
                intent = new Intent(this, SettingsActivity.class);
                startActivityForResult(intent,1);
                return true;
            case R.id.help_menu:
                intent = new Intent(this, HelpActivity.class);
                startActivityForResult(intent,2);
                return true;
            case R.id.logout_menu:
                intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                return true;
            default:
                return false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 1)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                //TODO: Changes saved
                Toast.makeText(this, "Changes to settings saved", Toast.LENGTH_LONG).show();
            }
            if(resultCode == Activity.RESULT_CANCELED)
            {
                //TODO: Changes cancelled
                Toast.makeText(this, "Changes to settings cancelled", Toast.LENGTH_LONG).show();
            }
        }

        if (requestCode == 2)
        {
            //TODO: Dont really think anything needs to happen here, returning from help screen
        }
    }
}

@@ -769,578 +769,8 @@
 y%0A%7B%0A
-    private final static String DB_NAME = %22biomap_couchbase%22;%0A    private Database database;%0A    private Manager manager;%0A%0A    public Database getDatabaseInstance() throws CouchbaseLiteException, IOException %7B%0A        if (database == null) %7B%0A            database =  getManagerInstance().getDatabase(DB_NAME);%0A        %7D%0A        return database;%0A    %7D%0A%0A    public Manager getManagerInstance() throws IOException %7B%0A        if (manager == null) %7B%0A            manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);%0A        %7D%0A        return manager;%0A    %7D%0A%0A
     

remove couchbase code to switch to SugarORM