/*
 * Copyright 2013 Urs Wolfer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.urswolfer.intellij.plugin.gerrit.ui.filter;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.util.Consumer;
import git4idea.history.wholeTree.BasePopupAction;
import org.jetbrains.annotations.Nullable;

/**
 * @author Thomas Forrer
 */
public class StatusFilter extends AbstractChangesFilter {
    private static final ImmutableList<Status> statuses = ImmutableList.of(
            new Status("All", null),
            new Status("Open", "open"),
            new Status("Merged", "merged"),
            new Status("Abandoned", "abandoned"),
            new Status("Drafts", "draft")
    );

    private Optional<Status> value = Optional.absent();

    public StatusFilter() {
        value = Optional.of(statuses.get(1));
    }

    @Override
    public AnAction getAction(final Project project) {
        return new StatusPopupAction(project, "Status:", "Status");
    }

    @Override
    @Nullable
    public String getSearchQueryPart() {
        if (value.isPresent() && value.get().forQuery.isPresent()) {
            return String.format("is:%s", value.get().forQuery.get());
        } else {
            return null;
        }
    }

    private static final class Status {
        String label;
        Optional<String> forQuery;

        private Status(String label, String forQuery) {
            this.label = label;
            this.forQuery = Optional.fromNullable(forQuery);
        }
    }

    public final class StatusPopupAction extends BasePopupAction {
        private final Project project;

        public StatusPopupAction(Project project, String labelText, String asTextLabel) {
            super(project, labelText, asTextLabel);
            this.project = project;
            myLabel.setText(value.get().label);
        }

        @Override
        protected void createActions(Consumer<AnAction> actionConsumer) {
            for (final Status status : statuses) {
                actionConsumer.consume(new DumbAwareAction(status.label) {
                    @Override
                    public void actionPerformed(AnActionEvent e) {
                        value = Optional.of(status);
                        myLabel.setText(status.label);
                        setChanged();
                        notifyObservers(project);
                    }
                });
            }
        }
    }
}

@@ -671,24 +671,102 @@
 on.base.
-Optional
+Joiner;%0Aimport com.google.common.base.Optional;%0Aimport com.google.common.base.Supplier
 ;%0Aimport
@@ -807,16 +807,55 @@
 leList;%0A
+import com.google.common.collect.Sets;%0A
 import c
@@ -1181,16 +1181,39 @@
 lable;%0A%0A
+import java.util.Set;%0A%0A
 /**%0A * @
@@ -1588,24 +1588,519 @@
 t%22)%0A    );%0A%0A
+    private static final Supplier%3CString%3E queryForAll = new Supplier%3CString%3E() %7B%0A        @Override%0A        public String get() %7B%0A            Set%3CString%3E queryForAll = Sets.newHashSet();%0A            for (Status status : statuses) %7B%0A                if (status.forQuery.isPresent()) %7B%0A                    queryForAll.add(String.format(%22is:%25s%22, status.forQuery.get()));%0A                %7D%0A            %7D%0A            return String.format(%22(%25s)%22, Joiner.on(%22+OR+%22).join(queryForAll));%0A        %7D%0A    %7D;%0A%0A
     private 
@@ -2471,12 +2471,28 @@
 nt()
- && 
+) %7B%0A            if (
 valu
@@ -2515,32 +2515,36 @@
 .isPresent()) %7B%0A
+    
             retu
@@ -2594,24 +2594,101 @@
 ery.get());%0A
+            %7D else %7B%0A                return queryForAll.get();%0A            %7D%0A
         %7D el
@@ -2782,16 +2782,22 @@
         
+final 
 String l
@@ -2810,16 +2810,22 @@
         
+final 
 Optional

fix status-filter option "All"