package com.jetbrains.edu.learning.stepic;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.edu.learning.ui.LoginPanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class LoginDialog extends DialogWrapper {
  protected final LoginPanel myLoginPanel;

  public LoginDialog() {
    super(false);
    myLoginPanel = new LoginPanel(this);
    setTitle("Login to Stepik");
    setOKButtonText("Login");
    init();
  }

  @NotNull
  protected Action[] createActions() {
    return new Action[]{getOKAction(), getCancelAction()};
  }

  @Override
  protected JComponent createCenterPanel() {
    return myLoginPanel.getContentPanel();
  }

  @Override
  protected String getHelpId() {
    return "login_to_stepic";
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myLoginPanel.getPreferableFocusComponent();
  }

  @Override
  protected void doOKAction() {
    if (!validateLoginAndPasswordFields()) return;
    final StepicUser user = EduStepicConnector.login(myLoginPanel.getLogin(), myLoginPanel.getPassword());
    if (user != null) {
      doJustOkAction();
    }
    else {
      setErrorText("Login failed");
    }
  }

  public boolean validateLoginAndPasswordFields() {
    if (StringUtil.isEmptyOrSpaces(myLoginPanel.getLogin())) {
      setErrorText("Please, enter your login");
      return false;
    }
    if (StringUtil.isEmptyOrSpaces(myLoginPanel.getPassword())) {
      setErrorText("Please, enter your password");
      return false;
    }
    return true;
  }

  protected void doJustOkAction() {
    super.doOKAction();
  }

  public void clearErrors() {
    setErrorText(null);
  }
}
@@ -37,16 +37,110 @@
 tepic;%0A%0A
+import com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.project.ProjectUtil;%0A
 import c
@@ -223,24 +223,76 @@
 StringUtil;%0A
+import com.jetbrains.edu.learning.StudyTaskManager;%0A
 import com.j
@@ -1325,16 +1325,268 @@
 tion();%0A
+      final Project project = ProjectUtil.guessCurrentProject(myLoginPanel.getContentPanel());%0A      user.setEmail(myLoginPanel.getLogin());%0A      user.setPassword(myLoginPanel.getPassword());%0A      StudyTaskManager.getInstance(project).setUser(user);%0A
     %7D%0A  

Fix EDU-630 Missing email in settings and login dialog in case I've already logged in