package kz.cafe.admin.notifications;

import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import kz.cafe.admin.*;
import kz.cafe.admin.panels.*;
import kz.cafe.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;

public class Notificator extends Thread{
   
   private AdminMain application;
   
   private long lNewOrdersPrev = 0;
   private long lNewReservationsPrev = 0;

   
   public Notificator(AdminMain application){
      this.application = application;
      
   }
   
   
   @Override
   public void run(){
      while(true){
         if(application.isRunning()){
         }
         else{
            //Let's stop
            break;
         }
      
         if(application.UID < 0){
            //no session yet
         }
         else{
            long lNewOrders = MongoService.ds.find(MOrder.class)
                                             .filter(MOrder.F_STATUS, OrderConstants.STATUS.NEW.ordinal())
                                             .filter(MOrder.F_DELETED, false)
                                             .field(MOrder.F_UID).notEqual(application.UID)
                                             //.order("-" + MOrder.F_ID)
                                             .countAll();
            
            long lNewReservations = MongoService.ds.find(MReservation.class)
                                                   .filter(MReservation.F_CONFIRMED, false)
                                                   .filter(MReservation.F_DELETED, false)
                                                   .field(MOrder.F_UID).notEqual(application.UID)
                                                   //.order("-" + MReservation.F_ID)
                                                   //.limit(1)
                                                   .countAll();
   
            long lNewOrdersCalculated = 0;
            long lNewReservationsCalculated = 0;
            
   
            if(lNewOrders > lNewOrdersPrev){
               lNewOrdersCalculated =  lNewOrders - lNewOrdersPrev;
            }
            lNewOrdersPrev = lNewOrders;
            
            if(lNewReservations > lNewReservationsPrev){
               lNewReservationsCalculated =  lNewReservations - lNewReservationsPrev;
            }
            lNewReservationsPrev = lNewReservations;
            
            
            if(lNewOrdersCalculated > 0){
               try{
                  
                  if(application.COMPONENT instanceof DBListPanel){
                     DBListPanel<?> panel = (DBListPanel<?>) application.COMPONENT;

                     if(panel.classDB == MOrder.class || panel.classDB == MReservation.class){
                        panel.reload();
                     }
                  }
               }
               catch (Throwable e){
               }
            }
                  
            if(lNewOrdersCalculated > 0 || lNewReservationsCalculated > 0) {
               final Window window = new Window("Оповещение");
               window.setModal(true);
               window.setResizable(false);
               //window.setSizeUndefined();
               //window.setHeight("20px");
               window.setWidth("500px");
               
               VerticalLayout layout = (VerticalLayout) window.getContent();
               layout.setMargin(false);
               layout.setSpacing(false);

               window.addComponent(AbstractPanel.sizeY(5));
               
               
               Label lbNewOrders = new Label(" Новых заказов: " + lNewOrdersCalculated + "      Новых мест забронировано: " + lNewReservationsCalculated);
               window.addComponent(lbNewOrders);
               
               Button btClose = new Button("Закрыть");
               btClose.addListener(new Button.ClickListener(){
                  private static final long serialVersionUID = 7530022888380402293L;

                  @Override
                  public void buttonClick(ClickEvent event){
                     application.getMainWindow().removeWindow(window);
                  }
               });
               window.addComponent(AbstractPanel.sizeY(10));
               
               HorizontalLayout layH = new HorizontalLayout();
               layH.setSizeFull();
               layH.addComponent(btClose);
               layH.setComponentAlignment(btClose, Alignment.MIDDLE_CENTER);
               window.addComponent(layH);
               
               window.addComponent(AbstractPanel.sizeY(10));
               
               Label lbSound = new Label("<audio autoplay><source src=\"" + ServerConstants.getAudioURL() + "/new_order.wav\" type=\"audio/wav\"></audio>", Label.CONTENT_XHTML);
               window.addComponent(lbSound);
               
               window.setVisible(true);
               application.getMainWindow().addWindow(window);
               
               //application.getMainWindow().showNotification("ПИЗДААААААААА");
               application.pusher.push();
               //System.out.println("NEW ORDDDDDDDDDDDDDDDER");
            }
         }
                  
         
         
         try{
            sleep(10000);
         }
         catch (InterruptedException e){
            break;
         }
      }
   }
}
