package kz.cafe.admin.autoimport;

import java.io.*;
import java.util.*;
import org.apache.commons.logging.*;
import kz.cafe.admin.*;
import kz.cafe.admin.panels.*;
import kz.cafe.db.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.FinishedEvent;
import com.vaadin.ui.Upload.StartedEvent;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Window.Notification;



public class TabAutoImport extends AbstractPanel implements java.io.Serializable{
   private static final long serialVersionUID = -8782438197035723526L;
   
   
   private static Log log = LogFactory.getLog(MongoService.class);
   private static final String LOG = "[UI Importer] ";
   

   private Upload upload;
   
   private File file; 


   public TabAutoImport(final AdminMain application){
      try{
         file = File.createTempFile("xls_auto_import", "xls");
      }
      catch (Throwable e){
         application.getMainWindow().showNotification("ERROR",
            "Не могу добавить создать временный файл " + e.toString(),
            Notification.TYPE_WARNING_MESSAGE
         );
         
         return;
      }
      
      
      AbstractLayout panelLayout = (AbstractLayout) getContent();
      panelLayout.setMargin(false);

      upload = new Upload(null, new Uploader());
      upload.setButtonCaption("XLS файл");
      upload.setImmediate(true);

      upload.addListener(new Upload.StartedListener(){
         private static final long serialVersionUID = 5813862327410567135L;





         public void uploadStarted(StartedEvent event){
            // This method gets called immediatedly after upload is started
            upload.setEnabled(false);
         }
      });

      
      
      upload.addListener(new Upload.SucceededListener(){
         private static final long serialVersionUID = 103112353892881253L;





         public void uploadSucceeded(SucceededEvent event){
            try{
               List<String> listErrors = Importer.importLogic(file.getAbsolutePath());
               
               if(listErrors.size() > 0) {
                  StringBuilder sbPizda = new StringBuilder();
                  
                  for(String sError : listErrors){
                     sbPizda.append(" | ");
                     sbPizda.append(sError);
                  }
                  
                  application.getMainWindow().showNotification("ERROR",
                                                               sbPizda.toString(),
                                                               Notification.TYPE_ERROR_MESSAGE
                                                            );
               }
            }
            catch (Throwable e){
               log.fatal(LOG + e.toString(), e);
               
               application.getMainWindow().showNotification("ERROR",
                  "Не могу добавить объект в базу " + e.toString(),
                  Notification.TYPE_ERROR_MESSAGE
               );
            }
         }
      });

      
      
      
      
      
      
      upload.addListener(new Upload.FailedListener(){
         private static final long serialVersionUID = 3536909203900982601L;

         public void uploadFailed(FailedEvent event){
            // table.getApplication().getMainWindow().showNotification(
            // "ERROR",
            // event.getSource().toString(),
            // Notification.TYPE_WARNING_MESSAGE);

            upload.setEnabled(true);
         }
      });

      
      
      
      
      upload.addListener(new Upload.FinishedListener(){
         private static final long serialVersionUID = 1031302920470671299L;

         public void uploadFinished(FinishedEvent event){
            upload.setEnabled(true);
         }
      });

      HorizontalLayout layH = new HorizontalLayout();

      this.addComponent(sizeY(15));
      
      layH.setSizeFull();
      layH.addComponent(upload);
      layH.setComponentAlignment(upload, Alignment.MIDDLE_CENTER);

      this.addComponent(layH);
      
      this.addComponent(sizeY(15));
   }

   
   

   
   
   
   
   
   private class Uploader implements Upload.SucceededListener, Upload.FailedListener, Upload.Receiver, Upload.FinishedListener{
      private static final long serialVersionUID = 1420577175001384829L;


      public OutputStream receiveUpload(String filename, String MIMEType){

         FileOutputStream fileOutputStream;
         try{
            fileOutputStream = new FileOutputStream(file.getAbsolutePath());
            return fileOutputStream;
         }
         catch (FileNotFoundException e){
            throw new Error("Не могу загрузить файл", e);
         }
      }


      public void uploadFailed(FailedEvent event){
      }
      public void uploadSucceeded(SucceededEvent event){
      }
      public void uploadFinished(FinishedEvent event){
      }
   }
}
