package pl.vehicle_history.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.joda.time.DateTime;

import pl.vehicle_history.api.exception.VehicleHistoryApiException;
import pl.vehicle_history.api.method.AuthMethod;
import pl.vehicle_history.api.method.GetVehicleMethod;
import pl.vehicle_history.api.method.ResponseListener;
import pl.vehicle_history.api.model.Auth;
import pl.vehicle_history.api.model.VehicleInput;
import pl.vehicle_history.api.model.VehicleResponse;
import pl.vehicle_history.fragment.AboutFragment;
import pl.vehicle_history.fragment.FindVehicleFragment;
import pl.vehicle_history.fragment.NavigationDrawerFragment;
import pl.vehicle_history.fragment.OptionsFragment;
import pl.vehicle_history.fragment.SearchHistoryFragment;
import pl.vehicle_history.historiapojazdu.R;

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment navigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(
                R.id.navigation_drawer);

        title = getTitle();

        // Set up the drawer.
        navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();

        Fragment fragmentToSwitch;

        switch (position) {
            case 0: {
                fragmentToSwitch = FindVehicleFragment.newInstance(0);
                break;
            }
            case 1: {
                fragmentToSwitch = SearchHistoryFragment.newInstance(1);
                break;
            }
            case 2: {
                fragmentToSwitch = OptionsFragment.newInstance(2);
                break;
            }
            case 3: {
                fragmentToSwitch = AboutFragment.newInstance(3);
                break;
            }
            default: {
                fragmentToSwitch = FindVehicleFragment.newInstance(0);
                break;
            }
        }

        fragmentManager.beginTransaction().replace(R.id.container, fragmentToSwitch).commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 0: {
                title = getString(R.string.title_section_find_vehicle);
                break;
            }
            case 1: {
                title = getString(R.string.title_section_history);
                break;
            }
            case 2: {
                title = getString(R.string.title_section_options);
                break;
            }
            case 3: {
                title = getString(R.string.title_section_about);
                break;
            }
            default: {
                title = getString(R.string.app_name);
                break;
            }

        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(title);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!navigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -31,16 +31,121 @@
 ivity;%0A%0A
+import android.content.ActivityNotFoundException;%0Aimport android.content.Intent;%0Aimport android.net.Uri;%0A
 import a
@@ -162,16 +162,16 @@
 Bundle;%0A
-
 import a
@@ -390,33 +390,8 @@
 ty;%0A
-import android.util.Log;%0A
 impo
@@ -447,412 +447,8 @@
 m;%0A%0A
-import org.joda.time.DateTime;%0A%0Aimport pl.vehicle_history.api.exception.VehicleHistoryApiException;%0Aimport pl.vehicle_history.api.method.AuthMethod;%0Aimport pl.vehicle_history.api.method.GetVehicleMethod;%0Aimport pl.vehicle_history.api.method.ResponseListener;%0Aimport pl.vehicle_history.api.model.Auth;%0Aimport pl.vehicle_history.api.model.VehicleInput;%0Aimport pl.vehicle_history.api.model.VehicleResponse;%0A
 impo
@@ -712,32 +712,87 @@
 istoryFragment;%0A
+import pl.vehicle_history.historiapojazdu.BuildConfig;%0A
 import pl.vehicl
@@ -1980,16 +1980,23 @@
 ToSwitch
+ = null
 ;%0A%0A     
@@ -2420,32 +2420,126 @@
       case 3: %7B%0A
+                showMarketAppIn();%0A                break;%0A            %7D%0A            case 4: %7B%0A
                 
@@ -2758,32 +2758,76 @@
    %7D%0A        %7D%0A%0A
+        if (fragmentToSwitch != null) %7B%0A    
         fragment
@@ -2901,24 +2901,34 @@
 ).commit();%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -4822,32 +4822,32 @@
 rue;%0A        %7D%0A%0A
-
         return s
@@ -4885,11 +4885,418 @@
 );%0A    %7D
+%0A%0A    private void showMarketAppIn() %7B%0A        try %7B%0A            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(%22market://details?id=%22 + BuildConfig.APPLICATION_ID)));%0A        %7D catch (ActivityNotFoundException e) %7B%0A            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(%22http://play.google.com/store/apps/details?id=%22%0A                    + BuildConfig.APPLICATION_ID)));%0A        %7D%0A    %7D
 %0A%7D%0A

Implement showing app in market.