package in.testpress.testpress.ui.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;

import in.testpress.core.TestpressSdk;
import in.testpress.core.TestpressSession;
import in.testpress.exam.TestpressExam;
import in.testpress.store.TestpressStore;
import in.testpress.testpress.R;
import in.testpress.testpress.TestpressApplication;
import in.testpress.testpress.TestpressServiceProvider;
import in.testpress.testpress.models.DaoSession;
import in.testpress.testpress.models.InstituteSettings;
import in.testpress.testpress.models.InstituteSettingsDao;
import in.testpress.testpress.ui.MainActivity;
import in.testpress.testpress.ui.ProfileDetailsActivity;
import in.testpress.testpress.util.CommonUtils;
import in.testpress.testpress.util.SafeAsyncTask;
import in.testpress.testpress.util.UIUtils;
import in.testpress.ui.UserDevicesActivity;

import static in.testpress.exam.network.TestpressExamApiClient.SUBJECT_ANALYTICS_PATH;
import static in.testpress.testpress.BuildConfig.BASE_URL;

public class HandleMainMenu {
    private Activity activity;
    private TestpressServiceProvider serviceProvider;
    private InstituteSettings instituteSettings;

    public HandleMainMenu(Activity activity, TestpressServiceProvider serviceProvider) {
        this.activity = activity;
        this.serviceProvider = serviceProvider;

        DaoSession daoSession =
                ((TestpressApplication) activity.getApplicationContext()).getDaoSession();
        InstituteSettingsDao instituteSettingsDao = daoSession.getInstituteSettingsDao();
        instituteSettings = instituteSettingsDao.queryBuilder()
                .where(InstituteSettingsDao.Properties.BaseUrl.eq(BASE_URL))
                .list().get(0);
    }

    public void handleMenuOptionClick(int itemId) {
        Intent intent;
        switch (itemId) {
            case R.id.share:
                shareApp();
                break;
            case R.id.rate_us:
                rateApp();
                break;
            case R.id.logout:
                ((MainActivity) activity).logout();
                break;
            case R.id.bookmarks:
                checkAuthenticatedUser(R.string.bookmarks);
                break;
            case R.id.analytics:
                checkAuthenticatedUser(R.string.analytics);
                break;
            case R.id.login_activity:
                intent = new Intent(activity, UserDevicesActivity.class);
                activity.startActivity(intent);
                break;
            case R.id.profile:
                intent = new Intent(activity, ProfileDetailsActivity.class);
                activity.startActivity(intent);
                break;
        }
    }

    void checkAuthenticatedUser(final int clickedMenuTitleResId) {
        if (!CommonUtils.isUserAuthenticated(activity)) {
            ((MainActivity) activity).logout();
            return;
        }
        if (TestpressSdk.hasActiveSession(activity)) {
            showSDK(clickedMenuTitleResId);
        } else {
            new SafeAsyncTask<Void>() {
                @Override
                public Void call() throws Exception {
                    serviceProvider.getService(activity);
                    return null;
                }

                @Override
                protected void onSuccess(Void aVoid) throws Exception {
                    showSDK(clickedMenuTitleResId);
                }
            }.execute();
        }
    }

    void showSDK(int clickedMenuTitleResId) {
        TestpressSession session = TestpressSdk.getTestpressSession(activity);
        assert session != null;
        switch (clickedMenuTitleResId) {
            case R.string.my_exams:
                TestpressExam.showCategories(activity, true, session);
                break;
            case R.string.bookmarks:
                TestpressExam.showBookmarks(activity, session);
                break;
            case R.string.analytics:
                TestpressExam.showAnalytics(activity, SUBJECT_ANALYTICS_PATH, session);
                break;
            case R.string.store:
                String title = UIUtils.getMenuItemName(R.string.store, instituteSettings);
                Intent intent = new Intent();
                intent.putExtra("title", title);
                activity.setIntent(intent);
                TestpressStore.show(activity, session);
                break;
        }
    }

    void rateApp() {
        Uri uri = Uri.parse("market://details?id=" + activity.getPackageName());
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        try {
            activity.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            activity.startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=" + activity.getPackageName())));
        }
    }

    void shareApp() {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, activity.getString(R.string.share_message) +
                activity.getString(R.string.get_it_at) + activity.getPackageName());
        activity.startActivity(Intent.createChooser(share, "Share with"));
    }
}

@@ -2231,38 +2231,42 @@
 checkAuthenticat
-edUser
+ionAndOpen
 (R.string.bookma
@@ -2359,22 +2359,26 @@
 henticat
-edUser
+ionAndOpen
 (R.strin
@@ -2822,14 +2822,18 @@
 icat
-edUser
+ionAndOpen
 (fin
@@ -2992,32 +2992,33 @@
 turn;%0A        %7D%0A
+%0A
         if (Test

Change method name