package com.neon.intellij.plugins.gitlab.view;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.neon.intellij.plugins.gitlab.*;
import com.neon.intellij.plugins.gitlab.model.gitlab.GIPGroup;
import com.neon.intellij.plugins.gitlab.model.gitlab.GIPIssue;
import com.neon.intellij.plugins.gitlab.model.gitlab.GIPProject;
import com.neon.intellij.plugins.gitlab.model.gitlab.GIPUser;
import com.neon.intellij.plugins.gitlab.model.intellij.GLProjectNode;
import com.neon.intellij.plugins.gitlab.view.toolwindow.GLIssueListView;
import com.neon.intellij.plugins.gitlab.view.toolwindow.GLIssuesFilterView;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class GitLabView extends JPanel implements GIPGroupObserver, GIPProjectObserver, GIPIssueObserver, GIPUserObserver {

    private final GLIssueListView glIssueListView ;

    private final GLIssuesFilterView glIssuesFilterView;

    public GitLabView(final OpenIssueEditorAction openIssueEditorAction,
                      final RefreshProjectIssuesAction refreshProjectIssuesAction,
                      final DeleteIssueAction deleteIssueAction,
                      final ChangeIssueStateAction changeIssueStateAction) {
        this.glIssueListView = new GLIssueListView( openIssueEditorAction, refreshProjectIssuesAction,
                deleteIssueAction, changeIssueStateAction);
        this.glIssuesFilterView = new GLIssuesFilterView( glIssueListView );

        this.setLayout( new BorderLayout( 5, 5 ) );
        this.add( glIssuesFilterView, BorderLayout.NORTH );
        this.add( buildActionsPanel( this, openIssueEditorAction, refreshProjectIssuesAction ), BorderLayout.WEST );
        this.add( glIssueListView, BorderLayout.CENTER );
    }


    private Component buildActionsPanel( final JComponent target,
                                         final OpenIssueEditorAction openIssueEditorAction,
                                         final RefreshProjectIssuesAction refreshProjectIssuesAction ) {
        final ActionManager actionManager = ActionManager.getInstance();

        final DefaultActionGroup actionGroup = new DefaultActionGroup();
        actionGroup.add( new AnAction( "Refresh All", "Refresh connection settings and projects list", AllIcons.Actions.Refresh ) {
            @Override
            public void actionPerformed(AnActionEvent anActionEvent) {
                GLProjectNode[] selected = glIssueListView.getSelectedNodes( GLProjectNode.class, null );
                if ( selected != null && selected.length > 0 ) {
                    for (GLProjectNode glProjectNode : selected) {
                        refreshProjectIssuesAction.accept( glProjectNode );
                    }
                } else {
                    refreshProjectIssuesAction.accept(null);
                }
            }
        });
        actionGroup.addSeparator();
        actionGroup.add(new AnAction( "New Issue", "", AllIcons.General.Add ) {
            @Override
            public void actionPerformed(AnActionEvent anActionEvent) {
                GLProjectNode[] selected = glIssueListView.getSelectedNodes( GLProjectNode.class, null );
                if ( selected != null && selected.length > 0 ) {
                    GLProjectNode node = selected[ 0 ];
                    GIPProject project = node.getUserObject();

                    GIPIssue issue = new GIPIssue();
                    issue.project_id = project.id;
                    openIssueEditorAction.accept( issue );
                }
            }
        });
//        actionGroup.addSeparator();
//        actionGroup.add( new AnAction( "Expand All", "", AllIcons.Actions.Expandall ) {
//            @Override
//            public void actionPerformed(AnActionEvent anActionEvent) {
////                TODO : to implement
//            }
//        });
//        actionGroup.add(new AnAction("Collapse All", "", AllIcons.Actions.Collapseall) {
//            @Override
//            public void actionPerformed(AnActionEvent anActionEvent) {
////                TODO : to implement
//            }
//        });
//        actionGroup.add( new AnAction( "Group By Module", "", AllIcons.Actions.GroupByModule ) {
//            @Override
//            public void actionPerformed(AnActionEvent anActionEvent) {
////                TODO : to implement
//            }
//        });

        ActionToolbar actionToolbar = actionManager.createActionToolbar("Gitlab Integration Toolbar", actionGroup, false);
        actionToolbar.setTargetComponent( target );
        return actionToolbar.getComponent();
    }


    @Override
    public void onStartGroupsUpdate() {
        glIssueListView.onStartGroupsUpdate();
    }

    @Override
    public void accept(GIPGroup group) {
        glIssueListView.accept( group );
    }

    @Override
    public void onStartProjectUpdate( GIPProject project ) {
        glIssueListView.onStartProjectUpdate( project );
    }

    @Override
    public void accept(GIPProject project) {
        glIssueListView.accept( project );
    }

    @Override
    public void accept(GIPIssue issue) {
        glIssueListView.accept( issue );
    }

    @Override
    public void onStartUsersUpdate() {
        glIssuesFilterView.onStartUsersUpdate();
    }

    @Override
    public void accept(List<GIPUser> users) {
        glIssuesFilterView.accept( users );
    }
}

@@ -1287,17 +1287,16 @@
 istView(
- 
 openIssu
@@ -2038,82 +2038,8 @@
 ) %7B%0A
-        final ActionManager actionManager = ActionManager.getInstance();%0A%0A
     
@@ -2850,32 +2850,33 @@
 actionGroup.add(
+ 
 new AnAction( %22N
@@ -2872,17 +2872,16 @@
 nAction(
- 
 %22New Iss
@@ -2909,17 +2909,16 @@
 eral.Add
- 
 ) %7B%0A    
@@ -3074,33 +3074,32 @@
 etSelectedNodes(
- 
 GLProjectNode.cl
@@ -3099,33 +3099,32 @@
 Node.class, null
- 
 );%0A             
@@ -3122,33 +3122,32 @@
             if (
- 
 selected != null
@@ -3161,33 +3161,32 @@
 ected.length %3E 0
- 
 ) %7B%0A            
@@ -3223,19 +3223,17 @@
 elected%5B
- 0 
+0
 %5D;%0A     
@@ -3440,34 +3440,248 @@
 tion.accept(
- issue 
+issue);%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void update(AnActionEvent e) %7B%0A                GLProjectNode%5B%5D glProjectNodes = glIssueListView.getSelectedNodes(GLProjectNode.class, null
 );%0A
+%0A
             
@@ -3676,33 +3676,177 @@
                 
-%7D
+Presentation presentation = e.getPresentation();%0A                presentation.setEnabled( glProjectNodes != null && glProjectNodes.length == 1 );
 %0A            %7D%0A 
@@ -3845,35 +3845,37 @@
      %7D%0A        %7D
+ 
 );%0A
+%0A
 //        action
@@ -4674,24 +4674,91 @@
        %7D);%0A%0A
+        ActionManager actionManager = ActionManager.getInstance();%0A
         Acti

update 'new issue' action state based on tree selection