package com.tasomaniac.openwith.resolver;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Resources;
import android.support.annotation.Nullable;

import com.tasomaniac.openwith.R;

import java.util.List;

import timber.log.Timber;

import static com.tasomaniac.openwith.data.OpenWithDatabase.OpenWithColumns.*;
import static com.tasomaniac.openwith.data.OpenWithProvider.OpenWithHosts.CONTENT_URI;

class DefaultResolverPresenter implements ResolverPresenter {

    private final Resources resources;
    private final ChooserHistory history;
    private final ContentResolver contentResolver;
    private final IntentResolver intentResolver;
    private final LastSelectedHolder lastSelectedHolder;

    DefaultResolverPresenter(Resources resources, ChooserHistory history, ContentResolver contentResolver, IntentResolver intentResolver, LastSelectedHolder lastSelectedHolder) {
        this.resources = resources;
        this.history = history;
        this.contentResolver = contentResolver;
        this.intentResolver = intentResolver;
        this.lastSelectedHolder = lastSelectedHolder;
    }

    @Override
    public void bind(ResolverView view) {
        view.setListener(new ViewListener(view, intentResolver, lastSelectedHolder));
        intentResolver.setListener(new IntentResolverListener(view));

        view.displayProgress();
        intentResolver.rebuildList();
    }

    @Override
    public void unbind(ResolverView view) {
        view.setListener(null);
        intentResolver.setListener(null);
    }

    private class IntentResolverListener implements IntentResolver.Listener {

        private final ResolverView view;

        IntentResolverListener(ResolverView view) {
            this.view = view;
        }

        @Override
        public void onIntentResolved(List<DisplayResolveInfo> list, @Nullable DisplayResolveInfo filteredItem, boolean showExtended) {
            int totalCount = list.size() + (filteredItem != null ? 1 : 0);
            if (totalCount == 0) {
                Timber.e("No app is found to handle url: %s", intentResolver.getSourceIntent().getDataString());
                view.toast(R.string.empty_resolver_activity);
                view.finish();
                return;
            }
            boolean hasFilteredItem = filteredItem != null;
            if (totalCount == 1) {
                DisplayResolveInfo dri = hasFilteredItem ? filteredItem : list.get(0);
                view.startWithMessage(dri.intentFrom(intentResolver.getSourceIntent()), dri.displayLabel());
                view.finish();
                return;
            }

            view.setResolvedList(list);
            view.setupList(filteredItem, showExtended);
            view.setTitle(titleForAction(filteredItem));
            if (hasFilteredItem) {
                view.setupFilteredView(filteredItem);

            }
            view.enableListSelection(!hasFilteredItem);
            view.setupActionButtons();
        }

        private String titleForAction(DisplayResolveInfo item) {
            return item != null ?
                    resources.getString(R.string.which_view_application_named, item.displayLabel()) :
                    resources.getString(R.string.which_view_application);
        }
    }

    private class ViewListener implements ResolverView.Listener {

        private final ResolverView view;
        private final IntentResolver intentResolver;
        private final LastSelectedHolder lastSelectedHolder;

        ViewListener(ResolverView view, IntentResolver intentResolver, LastSelectedHolder lastSelectedHolder) {
            this.view = view;
            this.intentResolver = intentResolver;
            this.lastSelectedHolder = lastSelectedHolder;
        }

        @Override
        public void onActionButtonClick(boolean always) {
            DisplayResolveInfo dri = shouldUseAlwaysOption() ? lastSelectedHolder.lastSelected : intentResolver.getFilteredItem();
            Intent intent = dri.intentFrom(intentResolver.getSourceIntent());
            view.startSelected(intent);
            persistSelectedIntent(intent, always);
        }

        private void persistSelectedIntent(Intent intent, boolean alwaysCheck) {
            if (intent.getComponent() == null) {
                return;
            }
            ContentValues values = new ContentValues(4);
            values.put(HOST, intent.getData().getHost());
            values.put(COMPONENT, intent.getComponent().flattenToString());

            if (alwaysCheck) {
                values.put(PREFERRED, true);
            }
            values.put(LAST_CHOSEN, true);
            try {
                contentResolver.insert(CONTENT_URI, values);
            } catch (Exception e) {
                Timber.e(e, "Error while saving selected Intent");
            }

            history.add(intent.getComponent().getPackageName());
            history.save();
        }

        @Override
        public void onItemClick(DisplayResolveInfo dri) {
            if (shouldUseAlwaysOption() && !dri.equals(lastSelectedHolder.lastSelected)) {
                view.enableActionButtons();
                lastSelectedHolder.lastSelected = dri;
            } else {
                Intent intent = dri.intentFrom(intentResolver.getSourceIntent());
                view.startSelected(intent);
            }
        }

        @Override
        public void onPackagesChanged() {
            view.displayProgress();
            intentResolver.rebuildList();
        }

        private boolean shouldUseAlwaysOption() {
            return !intentResolver.hasFilteredItem();
        }
    }
}

@@ -4058,154 +4058,27 @@
     
-Intent intent = dri.intentFrom(intentResolver.getSourceIntent());%0A            view.startSelected(intent);%0A            persistSelectedIntent(intent
+startAndPersist(dri
 , al
@@ -5176,24 +5176,174 @@
  else %7B%0A    
+            startAndPersist(dri, false);%0A            %7D%0A        %7D%0A%0A        private void startAndPersist(DisplayResolveInfo dri, boolean alwaysCheck) %7B%0A
             
@@ -5412,36 +5412,32 @@
 ));%0A            
-    
 view.startSelect
@@ -5452,33 +5452,75 @@
 t);%0A            
-%7D
+persistSelectedIntent(intent, alwaysCheck);
 %0A        %7D%0A%0A    

Call persist for list item selection too