/**
 * Copyright 2014 Microsoft Open Technologies Inc.
 *
 * 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.microsoftopentechnologies.intellij.wizards.createvm;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.ui.wizard.WizardNavigationState;
import com.intellij.ui.wizard.WizardStep;
import com.microsoftopentechnologies.intellij.forms.ManageSubscriptionForm;
import com.microsoftopentechnologies.intellij.helpers.UIHelper;
import com.microsoftopentechnologies.intellij.helpers.azure.AzureCmdException;
import com.microsoftopentechnologies.intellij.helpers.azure.AzureRestAPIManager;
import com.microsoftopentechnologies.intellij.model.Subscription;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;

public class SubscriptionStep extends WizardStep<CreateVMWizardModel> {

    CreateVMWizardModel model;
    private JPanel rootPanel;
    private JList createVmStepsList;
    private JButton buttonLogin;
    private JComboBox subscriptionComboBox;

    public SubscriptionStep(final CreateVMWizardModel model) {
        super("Choose a Subscription", null);

        this.model = model;

        model.configStepList(createVmStepsList, 0);


        buttonLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                ManageSubscriptionForm form = new ManageSubscriptionForm(null);
                UIHelper.packAndCenterJDialog(form);
                form.setVisible(true);

                loadSubscriptions();
            }
        });

        subscriptionComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                if (itemEvent.getItem() instanceof Subscription) {
                    model.setSubscription((Subscription) itemEvent.getItem());
                }
            }
        });


    }

    @Override
    public JComponent prepare(WizardNavigationState wizardNavigationState) {
        rootPanel.revalidate();

        loadSubscriptions();

        return rootPanel;
    }

    private void loadSubscriptions() {
        try {
            ArrayList<Subscription> subscriptionList = AzureRestAPIManager.getManager().getSubscriptionList();

            final Vector<Subscription> subscriptions = new Vector<Subscription>((subscriptionList == null) ? new Vector<Subscription>() : subscriptionList);
            subscriptionComboBox.setModel(new DefaultComboBoxModel(subscriptions));

            if(!subscriptions.isEmpty()) {
                model.setSubscription(subscriptions.get(0));
            }

            ApplicationManager.getApplication().invokeLater(new Runnable() {
                @Override
                public void run() {
                    model.getCurrentNavigationState().NEXT.setEnabled(!subscriptions.isEmpty());
                }
            });

        } catch (AzureCmdException e) {
            UIHelper.showException("Error retrieving subscription list", e);
        }
    }
}

@@ -685,121 +685,8 @@
 m;%0A%0A
-import com.intellij.openapi.application.Application;%0Aimport com.intellij.openapi.application.ApplicationManager;%0A
 impo
@@ -2691,24 +2691,94 @@
 iptions();%0A%0A
+        // model.getCurrentNavigationState().NEXT.setEnabled(false);%0A%0A
         retu
@@ -3328,155 +3328,9 @@
  %7D%0A%0A
-            ApplicationManager.getApplication().invokeLater(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A        
+%0A
     
@@ -3417,42 +3417,8 @@
 ());
-%0A                %7D%0A            %7D);
 %0A%0A  

Fix Issue #127