/*
 * The MIT License
 *
 * Copyright 2016 Pawel Marynowski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package pattypan.panes;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.Node;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import pattypan.Session;
import pattypan.elements.WikiButton;
import pattypan.elements.WikiLabel;
import pattypan.elements.WikiPane;

public class CreateFilePane extends WikiPane {

  Stage stage;

  WikiLabel descLabel;
  WikiButton createButton;

  public CreateFilePane(Stage stage) {
    super(stage, 1.0);
    this.stage = stage;

    setContent();
  }

  public WikiPane getContent() {
    return this;
  }
  
  private WikiPane setContent() {
    descLabel = new WikiLabel("You will create spreadsheet with " + Session.FILES.size()
            + " files from directory " + Session.DIRECTORY.getName() + ".").setWrapped(true);
    descLabel.setTextAlignment(TextAlignment.LEFT);
    addElement(descLabel);
    
    createButton = new WikiButton("Create", "inversed").setWidth(100);
    createButton.setOnAction(event -> {
      try {
        createSpreadsheet();
        addElement(new WikiLabel("Spreadsheet created."));
        addElement(getOpenFileButton());
      } catch (IOException | BiffException | WriteException ex) {
        Logger.getLogger(CreateFilePane.class.getName()).log(Level.SEVERE, null, ex);
      }
    });
    addElement(createButton);

    prevButton.linkTo("ChooseColumnsPane", stage);
    nextButton.setVisible(false);
    
    return this;
  }
  
  private Node getOpenFileButton() {
    WikiButton button = new WikiButton("Open file");
    button.setOnAction(event -> {
      try {
        Desktop.getDesktop().open(Session.FILE);
      } catch (IOException ex) {
        Logger.getLogger(CreateFilePane.class.getName()).log(Level.SEVERE, null, ex);
      }
    });
    return button;
  }
  
  private void createSpreadsheet() throws IOException, BiffException, WriteException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
    File f = new File(Session.DIRECTORY, "pattypan " + sdf.format(new Date()) + ".xls");
    WritableWorkbook workbook = Workbook.createWorkbook(f);
    
    WritableSheet sheet = workbook.createSheet("Data", 0);
    int num = 0;
    for(String var : Session.VARIABLES) {
      sheet.addCell(new Label(num++, 0, var));
    }
    
    num = 1;
    for(File file : Session.FILES) {
      sheet.addCell(new Label(0, num, file.getAbsolutePath()));
      sheet.addCell(new Label(1, num++, file.getName()));
    }
    
    WritableSheet templateSheet = workbook.createSheet("Template", 1);
    templateSheet.addCell(new Label(0, 0, Session.WIKICODE));
    
    workbook.write(); 
    workbook.close();
    Session.FILE = f;
  }
}

@@ -1224,32 +1224,90 @@
 io.IOException;%0A
+import java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A
 import java.util
@@ -1359,34 +1359,8 @@
 er;%0A
-import javafx.scene.Node;%0A
 impo
@@ -2083,16 +2083,43 @@
 kiLabel(
+String.format(%0A            
 %22You wil
@@ -2148,73 +2148,71 @@
 ith 
-%22 + Session.FILES.size()%0A            + %22 files from directory %22 +
+%25s files from directory %25s.%22,%0A            Session.FILES.size(),
  Ses
@@ -2239,14 +2239,14 @@
 me()
- + %22.%22
+%0A    )
 ).se
@@ -2390,20 +2390,24 @@
 eate
-%22, %22inversed
+ file%22, %22primary
 %22).s
@@ -2414,17 +2414,17 @@
 etWidth(
-1
+2
 00);%0A   
@@ -2554,16 +2554,29 @@
  created
+ successfully
 .%22));%0A  
@@ -2581,27 +2581,16 @@
         
-addElement(
 getOpenF
@@ -2600,17 +2600,16 @@
 Button()
-)
 ;%0A      
@@ -2660,32 +2660,122 @@
 Exception ex) %7B%0A
+        addElement(new WikiLabel(%22Error occurred during creation of spreadsheet file!%22));%0A
         Logger.g
@@ -3019,12 +3019,12 @@
 ate 
-Node
+void
  get
@@ -3050,62 +3050,83 @@
     
-Wiki
+next
 Button
- button = new WikiButton(%22Open file%22
+.setText(%22Open file%22);%0A    nextButton.setVisible(true
 );%0A    
-b
+nextB
 utto
@@ -3350,27 +3350,8 @@
 %7D);%0A
-    return button;%0A
   %7D%0A

clean up create file pane