package kz.cafe.admin;

import kz.cafe.admin.panels.*;
import kz.cafe.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import kz.cafe.db.service.*;
import kz.cafe.exceptions.*;
import com.vaadin.event.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;



public class LoginPage extends AbstractPanel{
   private static final long serialVersionUID = -8000482966997452676L;

   private final TextField tfLogin = new TextField();
   private final TextField tfPassword = new TextField();
   private final Button btLogin = new Button("Вход");



   private final AdminMain application;


   public LoginPage(AdminMain application){
      VerticalLayout mainLayout = new VerticalLayout();
      mainLayout.setSizeFull();
      this.setContent(mainLayout);      
      

      if(ServerConstants.isDebug()){
         MUser mUser = MongoService.get(MUser.class, 0);
         tfLogin.setValue(mUser.login);
         tfPassword.setValue(mUser.password);
      }

      this.application = application;

      GridLayout layG = new GridLayout(3, 3);
      

      layG.addComponent(new Label("Login:"));
      layG.addComponent(sizeX(5));

      tfLogin.addShortcutListener(new ShortcutListener("Shortcut Name",
                                  ShortcutAction.KeyCode.ENTER,
                                  null){
         private static final long serialVersionUID = -3325764643223030355L;

         @Override
         public void handleAction(Object sender, Object target){
            login();
         }
      });


      layG.addComponent(tfLogin);

      layG.addComponent(sizeY(10));

      layG.addComponent(new Label("Пароль:"), 0, 2);
      layG.addComponent(sizeX(5), 1, 2);

      tfPassword.setSecret(true);
      tfPassword.addShortcutListener(new ShortcutListener("Shortcut Name",
                                     ShortcutAction.KeyCode.ENTER,
                                     null){
         private static final long serialVersionUID = -3325764643223030355L;

         @Override
          public void handleAction(Object sender, Object target) {
            login();
          }
      });

      layG.addComponent(tfPassword, 2, 2);

      mainLayout.addComponent(AbstractPanel.sizeY(20));
      mainLayout.addComponent(layG);
      mainLayout.setComponentAlignment(layG, Alignment.TOP_CENTER);
      

      this.addComponent(sizeY(10));

      btLogin.addListener(new ClickLogin());

      mainLayout.addComponent(btLogin);
      mainLayout.setComponentAlignment(btLogin, Alignment.TOP_CENTER);
      
      this.setStyleName("panel-header");
      
   }





   public class ClickLogin implements Button.ClickListener{
      private static final long serialVersionUID = 3533359174958383113L;

      public void buttonClick(ClickEvent event){
         login();
      }
   }







   private void login(){
      String sLogin    = "" + tfLogin.getValue();
      String sPassword = "" + tfPassword.getValue();
      
      MUser mUser;
      try{
         mUser = MongoUserService.getUserProfile(sLogin);
         
         if(!mUser.password.equals(sPassword)){
            application.getMainWindow().showNotification("Неверный пароль",
                                                         Notification.TYPE_WARNING_MESSAGE);
            return;
         }
      }
      catch (LoginNotFoundException e){
         application.getMainWindow().showNotification("Нет такого пользователя",
                                                      Notification.TYPE_WARNING_MESSAGE);
         return;
      }

      
      if(mUser.canUseGUI){
      }
      else{
         application.getMainWindow().showNotification("Этот пользователь не может пользоваться панелью управления",
                                                          Notification.TYPE_WARNING_MESSAGE);
         return;
      }
      
      
      if(mUser.active){
      }
      else{
         application.getMainWindow().showNotification("Пользователь отключен",
                                                      Notification.TYPE_WARNING_MESSAGE);
         return;
      }
      
      
      
      application.UID   = mUser.ID;
      
      MRole role  = MongoService.get(MRole.class, mUser.role);
      
      if(role == null){
         application.getMainWindow().showNotification("Пользователь не может войти в систему без Роли",
                                                       Notification.TYPE_WARNING_MESSAGE);
         
         return;
      }
      
      application.isAdmin      = (role.ID == 0);
      application.screenRead   = role.screenRead;
      application.screenAdd    = role.screenAdd;
      application.screenEdit   = role.screenEdit;
      application.screenDelete = role.screenDelete;
      
      application.getMainWindow().removeComponent(LoginPage.this);
      
      
      application.buildMenu();
//      application.getMainWindow().addComponent(application.menubar);
   }
}
