package Windows;

import CalculationAndLampsSets.LampsSets;
import CalculationAndLampsSets.fluxCalculation;
import CommunicationAndSaving.propertiesFile;
import CommunicationAndSaving.saveFiles;
import CommunicationAndSaving.serialCommunication;
import measurementThread.ThreadM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;

import static Windows.Window.factor;

public class TableWindow extends JDialog {

    private serialCommunication serial;
    private propertiesFile properties = propertiesFile.getInstance();
    private Window window;
    private JTable jTable;
    private fluxCalculation fCalculation;
    private Vector<String> columnNamesVector;
    private Vector<Vector<String>> dataVector;
    private JButton saveCsv;
    private JButton saveLdt;
    private saveFiles saveLdtFile;
    private saveFiles saveCsvFile;
    private Random random;
    private JPanel savePanel;
    private JPanel tablePanel;
    private ArrayList<ArrayList<String>> values;
    public static double flux;

    /*
    Konštruktor triedy .. inicializuje a zobrazuje okno, s
    tabuľkou obsahujúcou hodnoty "svietivosti" ...
     */
    public TableWindow(JFrame Window, Window window) throws IOException {
        super(Window, "Table of Results", true);
        this.window = window;
        setSize(525, 525);
        setResizable(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        values = editMeasurementValuesTo2DArrayList(editMeasurementValues(ThreadM.getMeasurementStaticSetsForResultsWindow()));
        dataVector = new Vector<>(create2DVectorListData(values));
        columnNamesVector = new Vector<>(createArrayListColumnNames());

        jTable = new JTable(dataVector, columnNamesVector);
        JScrollPane tableScrollPane = new JScrollPane(jTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        savePanel = new JPanel();
        tablePanel = new JPanel();

        saveCsv = new JButton("Save .csv");
        saveCsv.addActionListener(new saveCSVFile());
        savePanel.add(saveCsv);
        saveLdt = new JButton("Save .ldt");
        saveLdt.addActionListener(new saveLDTFile());
        savePanel.add(saveLdt);

        tablePanel.add(tableScrollPane);

        add(savePanel, BorderLayout.NORTH);
        add(tablePanel, BorderLayout.SOUTH);
        random = new Random();

        fCalculation = new fluxCalculation(window, values);
        flux = fCalculation.calculate();
        window.show("\n" + "Flux of sorce: " + flux);
    }

    private ArrayList<String> createArrayListColumnNames() {
        ArrayList<String> listEnd = new ArrayList<>();

        for (int i = 0; i < ThreadM.getMeasurementStaticSetsForResultsWindow().get(0).size()/2 + 1; i++) {

            if (i == 0){
                listEnd = new ArrayList<>();
                listEnd.add("");
            }
            listEnd.add(String.valueOf(i * window.gammaFactor));
        }
        return listEnd;
    }

    private Vector<Vector<String>> create2DVectorListData(ArrayList<ArrayList<String>> arrayList){
        Vector<Vector<String>> twoDimensionsVector = new Vector<>();
        Vector<String> eachVector = new Vector<>();
        Vector<String> help = null;

        int j = 0;
        for(ArrayList<String> list : arrayList){
            for (int i = 0; i < list.size(); i++) {
                if (i == 0){
                    eachVector.add("C" + String.valueOf((j * factor)));
                    j++;
                }
                eachVector.add(list.get(i));
            }
            help = (Vector<String>) eachVector.clone();
            //twoDimensionsVector.add(columnNames2DVector);
            twoDimensionsVector.add(help);
            eachVector.clear();
        }
        return twoDimensionsVector;
    }

    /*
    Trieda ... stlačenie tlačidla Save .csv, vedie k uloženiu
    nameraných hodnôt do textového súbora (.csv)
     */
    private class saveCSVFile implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (properties.get_Property("ID11_fileName").equalsIgnoreCase("") || properties.get_Property("ID11_fileName").equalsIgnoreCase("-")){
                    saveCsvFile = new saveFiles(properties.get_Property("File Path") + random.nextInt(1000) + "//" + properties.get_Property("ID11_fileName"), true);
                }else {
                    saveCsvFile = new saveFiles(properties.get_Property("File Path") + "\\" + properties.get_Property("ID11_fileName"), true);
                }
                fileCsvContent();
                saveCsvFile.closeFile();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(TableWindow.this, "Can not generate the .csv file!");
                System.out.println(e1);
            }
        }
    }

    /*
    Trieda ... stlačenie tlačidla Save .ldt, vedie k uloženiu
    nameraných hodnôt do textového súbora (.ldt)
     */
    private class saveLDTFile implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try{
                if (properties.get_Property("ID11_fileName").equalsIgnoreCase("") || properties.get_Property("ID11_fileName").equalsIgnoreCase("-")){
                    saveLdtFile = new saveFiles(properties.get_Property("File Path") + random.nextInt(1000) + "//" + properties.get_Property("ID11_fileName"));

                }else{
                    saveLdtFile = new saveFiles(properties.get_Property("File Path") + "\\" + properties.get_Property("ID11_fileName"));
                }
                fileLdtContent();
                saveLdtFile.closeFile();
            }catch(Exception e1){
                JOptionPane.showMessageDialog(TableWindow.this, "Can not generate the .ldt file!");
                System.out.println(e1);
            }
        }
    }

    /*
    Funkcia vytvára obsah, ktorý sa následne ukladá
    do .csv textového súbora ...
     */
    private void fileCsvContent() throws IOException {

        for (String string : columnNamesVector) {
            saveCsvFile.captureString(string);
            saveCsvFile.appendCsvSeparator();
        }
        saveCsvFile.enterNewLine();

        for(int i = 0; i < dataVector.size(); i++){
            for (int j = 0; j < dataVector.get(i).size(); j++){

                if (window.getIluminationForLDT() && !window.getCalculateFlux()){
                    if(j != 0){
                        saveCsvFile.captureString(formatForEulumdatValues(dataVector.get(i).get(j)));
                    }else {
                        saveCsvFile.captureString(dataVector.get(i).get(j));
                    }
                }else if (window.getCalculateFlux() && !window.getIluminationForLDT()){
                    if(j != 0){
                        saveCsvFile.captureString(formatForEulumdatValuesCalFlux(dataVector.get(i).get(j)));
                    }else {
                        saveCsvFile.captureString(dataVector.get(i).get(j));
                    }
                }else if (!window.getIluminationForLDT() && !window.getCalculateFlux()){
                    saveCsvFile.captureString(dataVector.get(i).get(j));
                }
                saveCsvFile.appendCsvSeparator();
            }
            saveCsvFile.enterNewLine();
        }

        for (int i = 0; i < 2; i++){
            saveCsvFile.enterNewLine();
            saveCsvFile.appendCsvSeparator();
        }
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID1_manufacturer");
        saveCsvFile.appendCsvSeparator();
        if(window.getID1_manufacturer().equalsIgnoreCase("") || window.getID1_manufacturer().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID1_manufacturer"));
        }else{
            saveCsvFile.captureString(window.getID1_manufacturer());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID2_type");
        saveCsvFile.appendCsvSeparator();
        try {
            saveCsvFile.captureString(window.getID2_type());
        }catch (Exception e){
            saveCsvFile.captureString(String.valueOf(Integer.parseInt(properties.get_Property("ID2_type"))+1));
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID3_symmetry");
        saveCsvFile.appendCsvSeparator();
        try {
            saveCsvFile.captureString(window.getID3_symmetry());
        }catch (Exception e){
            saveCsvFile.captureString(properties.get_Property("ID3_symmetry"));
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID5_Dc");
        saveCsvFile.appendCsvSeparator();
        if(window.getID5_Dc().equalsIgnoreCase("")){
            saveCsvFile.captureString(String.valueOf(360/Integer.parseInt(properties.get_Property("ID5_Dc"))));
        }else{
            saveCsvFile.captureString(String.valueOf(360/Integer.parseInt(window.getID5_Dc())));
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID5_Dc");
        saveCsvFile.appendCsvSeparator();
        if(window.getID5_Dc().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID5_Dc"));
        }else{
            saveCsvFile.captureString(window.getID5_Dc());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID7_Dg");
        saveCsvFile.appendCsvSeparator();
        if(window.getID7_Dg().equalsIgnoreCase("")){
            saveCsvFile.captureString(String.valueOf((180/Integer.parseInt(properties.get_Property("ID7_Dg")))+1));
        }else{
            saveCsvFile.captureString(String.valueOf((180/Integer.parseInt(window.getID7_Dg())) + 1));
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID7_Dg");
        saveCsvFile.appendCsvSeparator();
        if(window.getID7_Dg().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID7_Dg"));
        }else{
            saveCsvFile.captureString(window.getID7_Dg());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID8_measurementNumber");
        saveCsvFile.appendCsvSeparator();
        if(window.getID8_measurementNumber().equalsIgnoreCase("") || window.getID8_measurementNumber().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID8_measurementNumber"));
        }else{
            saveCsvFile.captureString(window.getID8_measurementNumber());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID9_luminaireName");
        saveCsvFile.appendCsvSeparator();
        if(window.getID9_luminaireName().equalsIgnoreCase("") || window.getID9_luminaireName().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID9_luminaireName"));
        }else{
            saveCsvFile.captureString(window.getID9_luminaireName());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID10_luminaireNumber");
        saveCsvFile.appendCsvSeparator();
        if(window.getID10_luminaireNumber().equalsIgnoreCase("") || window.getID10_luminaireNumber().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID10_luminaireNumber"));
        }else{
            saveCsvFile.captureString(window.getID10_luminaireNumber());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID11_fileName");
        saveCsvFile.appendCsvSeparator();
        if(window.getID11_fileName().equalsIgnoreCase("") || window.getID11_fileName().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID11_fileName"));
        }else{
            saveCsvFile.captureString(window.getID11_fileName());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID12_dateName");
        saveCsvFile.appendCsvSeparator();
        if(window.getID12_dateName().equalsIgnoreCase("") || window.getID12_dateName().equalsIgnoreCase("-")){
            saveCsvFile.captureString(properties.get_Property("ID12_dateName"));
        }else{
            saveCsvFile.captureString(window.getID12_dateName());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID13_luminaireLength");
        saveCsvFile.appendCsvSeparator();
        if(window.getID13_luminaireLength().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID13_luminaireLength"));
        }else{
            saveCsvFile.captureString(window.getID13_luminaireLength());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID14_luminaireWidth");
        saveCsvFile.appendCsvSeparator();
        if(window.getID14_luminaireWidth().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID14_luminaireWidth"));
        }else{
            saveCsvFile.captureString(window.getID14_luminaireWidth());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID15_luminaireHeight");
        saveCsvFile.appendCsvSeparator();
        if(window.getID15_luminaireHeight().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID15_luminaireHeight"));
        }else{
            saveCsvFile.captureString(window.getID15_luminaireHeight());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID16_luminaireArea");
        saveCsvFile.appendCsvSeparator();
        if(window.getID16_luminaireArea().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID16_luminaireArea"));
        }else{
            saveCsvFile.captureString(window.getID16_luminaireArea());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID17_widthLuminaireArea");
        saveCsvFile.appendCsvSeparator();
        if(window.getID17_widthLuminaireArea().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID17_widthLuminaireArea"));
        }else{
            saveCsvFile.captureString(window.getID17_widthLuminaireArea());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID18_heightLiminaireAreaC0");
        saveCsvFile.appendCsvSeparator();
        if(window.getID18_heightLiminaireAreaC0().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID18_heightLiminaireAreaC0"));
        }else{
            saveCsvFile.captureString(window.getID18_heightLiminaireAreaC0());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID19_heightLiminaireAreaC90");
        saveCsvFile.appendCsvSeparator();
        if(window.getID19_heightLiminaireAreaC90().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID19_heightLiminaireAreaC90"));
        }else{
            saveCsvFile.captureString(window.getID19_heightLiminaireAreaC90());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID20_heightLiminaireAreaC180");
        saveCsvFile.appendCsvSeparator();
        if(window.getID20_heightLiminaireAreaC180().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID20_heightLiminaireAreaC180"));
        }else{
            saveCsvFile.captureString(window.getID20_heightLiminaireAreaC180());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID21_heightLiminaireAreaC270");
        saveCsvFile.appendCsvSeparator();
        if(window.getID21_heightLiminaireAreaC270().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID21_heightLiminaireAreaC270"));
        }else{
            saveCsvFile.captureString(window.getID21_heightLiminaireAreaC270());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID22_DFF");
        saveCsvFile.appendCsvSeparator();
        if(window.getID22_DFF().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID22_DFF"));
        }else{
            saveCsvFile.captureString(window.getID22_DFF());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID23_LORL");
        saveCsvFile.appendCsvSeparator();
        if(window.getID23_LORL().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID23_LORL"));
        }else{
            saveCsvFile.captureString(window.getID23_LORL());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID24_conversionFactor");
        saveCsvFile.appendCsvSeparator();
        if(window.getID24_conversionFactor().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID24_conversionFactor"));
        }else{
            saveCsvFile.captureString(window.getID24_conversionFactor());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("ID25_tiltLuinaire");
        saveCsvFile.appendCsvSeparator();
        if(window.getID25_tiltLuinaire().equalsIgnoreCase("")){
            saveCsvFile.captureString(properties.get_Property("ID25_tiltLuinaire"));
        }else{
            saveCsvFile.captureString(window.getID25_tiltLuinaire());
        }
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        saveCsvFile.captureString("Lamp sets");
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.captureString(String.valueOf(Window.getID26_lamps().size()));
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        for (LampsSets set : Window.getID26_lamps()) {
            saveCsvFile.captureString(set.ID26a_numberOfLamp);
            saveCsvFile.appendCsvSeparator();
            saveCsvFile.captureString(set.ID26b_typeOfLamp);
            saveCsvFile.appendCsvSeparator();
            saveCsvFile.captureString(set.ID26c_fluxOfLamp);
            saveCsvFile.appendCsvSeparator();
            saveCsvFile.captureString(set.ID26d_ColorTemperatureOfLamp);
            saveCsvFile.appendCsvSeparator();
            saveCsvFile.captureString(set.ID26e_colorRenderingIndex);
            saveCsvFile.appendCsvSeparator();
            saveCsvFile.captureString(set.ID26f_allPowerSupply);
            saveCsvFile.appendCsvSeparator();
        }
        saveCsvFile.enterNewLine();

        ArrayList<String> ID27_DR = new ArrayList<>();
        if(window.getID27_DR_k060().equalsIgnoreCase("") || window.getID27_DR_k080().equalsIgnoreCase("") || window.getID27_DR_k100().equalsIgnoreCase("") || window.getID27_DR_k125().equalsIgnoreCase("") || window.getID27_DR_k150().equalsIgnoreCase("") || window.getID27_DR_k200().equalsIgnoreCase("") || window.getID27_DR_k250().equalsIgnoreCase("") || window.getID27_DR_k300().equalsIgnoreCase("") || window.getID27_DR_k400().equalsIgnoreCase("") || window.getID27_DR_k500().equalsIgnoreCase("")){
            ID27_DR.add(properties.get_Property("ID27_DR_k060"));
            ID27_DR.add(properties.get_Property("ID27_DR_k080"));
            ID27_DR.add(properties.get_Property("ID27_DR_k100"));
            ID27_DR.add(properties.get_Property("ID27_DR_k125"));
            ID27_DR.add(properties.get_Property("ID27_DR_k150"));
            ID27_DR.add(properties.get_Property("ID27_DR_k200"));
            ID27_DR.add(properties.get_Property("ID27_DR_k250"));
            ID27_DR.add(properties.get_Property("ID27_DR_k300"));
            ID27_DR.add(properties.get_Property("ID27_DR_k400"));
            ID27_DR.add(properties.get_Property("ID27_DR_k500"));
        }else{
            ID27_DR.add(window.getID27_DR_k060());
            ID27_DR.add(window.getID27_DR_k080());
            ID27_DR.add(window.getID27_DR_k100());
            ID27_DR.add(window.getID27_DR_k125());
            ID27_DR.add(window.getID27_DR_k150());
            ID27_DR.add(window.getID27_DR_k200());
            ID27_DR.add(window.getID27_DR_k250());
            ID27_DR.add(window.getID27_DR_k300());
            ID27_DR.add(window.getID27_DR_k400());
            ID27_DR.add(window.getID27_DR_k500());
        }

        saveCsvFile.captureString("ID27");
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();

        for (String string : ID27_DR) {
            saveCsvFile.captureString(string);
            saveCsvFile.appendCsvSeparator();
        }

        saveCsvFile.captureString("Calculated Flux: ");
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.captureString(String.valueOf(getFluxCalValue()));
        saveCsvFile.appendCsvSeparator();
        saveCsvFile.enterNewLine();
    }

    /*
    Funkcia vytvára obsah, ktorý sa následne ukladá
    do .ldt textového súbora ...
     */
    private void fileLdtContent() throws IOException {

        if(window.getID1_manufacturer().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID1_manufacturer"));
        }else{
            saveLdtFile.captureString(window.getID1_manufacturer());
        }
        saveLdtFile.appendLdtSeparator();

        try {
            saveLdtFile.captureString(window.getID2_type());
        }catch (Exception e){
            saveCsvFile.captureString(String.valueOf(Integer.parseInt(properties.get_Property("ID2_type"))+1));
        }
        saveLdtFile.appendLdtSeparator();

        try {
            saveLdtFile.captureString(window.getID3_symmetry());
        }catch (Exception e){
            saveLdtFile.captureString(properties.get_Property("ID3_symmetry"));
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID5_Dc().equalsIgnoreCase("")){
            saveLdtFile.captureString(String.valueOf(360/Integer.parseInt(properties.get_Property("ID5_Dc"))));
        }else{
            saveLdtFile.captureString(String.valueOf(360/Integer.parseInt(window.getID5_Dc())));
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID5_Dc().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID5_Dc"));
        }else{
            saveLdtFile.captureString(window.getID5_Dc());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID7_Dg().equalsIgnoreCase("")){
            saveLdtFile.captureString(String.valueOf((180/Integer.parseInt(properties.get_Property("ID7_Dg")))+1));
        }else{
            saveLdtFile.captureString(String.valueOf((180/Integer.parseInt(window.getID7_Dg()))+1));
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID7_Dg().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID7_Dg"));
        }else{
            saveLdtFile.captureString(window.getID7_Dg());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID8_measurementNumber().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID8_measurementNumber"));
        }else{
            saveLdtFile.captureString(window.getID8_measurementNumber());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID9_luminaireName().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID9_luminaireName"));
        }else{
            saveLdtFile.captureString(window.getID9_luminaireName());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID10_luminaireNumber().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID10_luminaireNumber"));
        }else{
            saveLdtFile.captureString(window.getID10_luminaireNumber());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID11_fileName().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID11_fileName"));
        }else{
            saveLdtFile.captureString(window.getID11_fileName());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID12_dateName().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID12_dateName"));
        }else{
            saveLdtFile.captureString(window.getID12_dateName());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID13_luminaireLength().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID13_luminaireLength"));
        }else{
            saveLdtFile.captureString(window.getID13_luminaireLength());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID14_luminaireWidth().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID14_luminaireWidth"));
        }else{
            saveLdtFile.captureString(window.getID14_luminaireWidth());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID15_luminaireHeight().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID15_luminaireHeight"));
        }else{
            saveLdtFile.captureString(window.getID15_luminaireHeight());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID16_luminaireArea().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID16_luminaireArea"));
        }else{
            saveLdtFile.captureString(window.getID16_luminaireArea());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID17_widthLuminaireArea().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID17_widthLuminaireArea"));
        }else{
            saveLdtFile.captureString(window.getID17_widthLuminaireArea());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID18_heightLiminaireAreaC0().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID18_heightLiminaireAreaC0"));
        }else{
            saveLdtFile.captureString(window.getID18_heightLiminaireAreaC0());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID19_heightLiminaireAreaC90().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID19_heightLiminaireAreaC90"));
        }else{
            saveLdtFile.captureString(window.getID19_heightLiminaireAreaC90());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID20_heightLiminaireAreaC180().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID20_heightLiminaireAreaC180"));
        }else{
            saveLdtFile.captureString(window.getID20_heightLiminaireAreaC180());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID21_heightLiminaireAreaC270().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID21_heightLiminaireAreaC270"));
        }else{
            saveLdtFile.captureString(window.getID21_heightLiminaireAreaC270());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID22_DFF().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID22_DFF"));
        }else{
            saveLdtFile.captureString(window.getID22_DFF());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID23_LORL().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID23_LORL"));
        }else{
            saveLdtFile.captureString(window.getID23_LORL());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID24_conversionFactor().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID24_conversionFactor"));
        }else{
            saveLdtFile.captureString(window.getID24_conversionFactor());
        }
        saveLdtFile.appendLdtSeparator();

        if(window.getID25_tiltLuinaire().equalsIgnoreCase("")){
            saveLdtFile.captureString(properties.get_Property("ID25_tiltLuinaire"));
        }else{
            saveLdtFile.captureString(window.getID25_tiltLuinaire());
        }
        saveLdtFile.appendLdtSeparator();

        saveLdtFile.captureString(String.valueOf(Window.getID26_lamps().size()));
        saveLdtFile.appendLdtSeparator();

        for (LampsSets set : Window.getID26_lamps()) {
            saveLdtFile.captureString(set.ID26a_numberOfLamp);
            saveLdtFile.appendLdtSeparator();
            saveLdtFile.captureString(set.ID26b_typeOfLamp);
            saveLdtFile.appendLdtSeparator();
            if (window.getCalculateFlux()){
                saveLdtFile.captureString(String.valueOf(getFluxCalValue()));
            }else{
                saveLdtFile.captureString(set.ID26c_fluxOfLamp);
            }
            saveLdtFile.appendLdtSeparator();
            saveLdtFile.captureString(set.ID26d_ColorTemperatureOfLamp);
            saveLdtFile.appendLdtSeparator();
            saveLdtFile.captureString(set.ID26e_colorRenderingIndex);
            saveLdtFile.appendLdtSeparator();
            saveLdtFile.captureString(set.ID26f_allPowerSupply);
            saveLdtFile.appendLdtSeparator();
        }

        ArrayList<String> ID27_DR = new ArrayList<>();
        if(window.getID27_DR_k060().equalsIgnoreCase("") || window.getID27_DR_k080().equalsIgnoreCase("") || window.getID27_DR_k100().equalsIgnoreCase("") || window.getID27_DR_k125().equalsIgnoreCase("") || window.getID27_DR_k150().equalsIgnoreCase("") || window.getID27_DR_k200().equalsIgnoreCase("") || window.getID27_DR_k250().equalsIgnoreCase("") || window.getID27_DR_k300().equalsIgnoreCase("") || window.getID27_DR_k400().equalsIgnoreCase("") || window.getID27_DR_k500().equalsIgnoreCase("")){
            ID27_DR.add(properties.get_Property("ID27_DR_k060"));
            ID27_DR.add(properties.get_Property("ID27_DR_k080"));
            ID27_DR.add(properties.get_Property("ID27_DR_k100"));
            ID27_DR.add(properties.get_Property("ID27_DR_k125"));
            ID27_DR.add(properties.get_Property("ID27_DR_k150"));
            ID27_DR.add(properties.get_Property("ID27_DR_k200"));
            ID27_DR.add(properties.get_Property("ID27_DR_k250"));
            ID27_DR.add(properties.get_Property("ID27_DR_k300"));
            ID27_DR.add(properties.get_Property("ID27_DR_k400"));
            ID27_DR.add(properties.get_Property("ID27_DR_k500"));
        }else{
            ID27_DR.add(window.getID27_DR_k060());
            ID27_DR.add(window.getID27_DR_k080());
            ID27_DR.add(window.getID27_DR_k100());
            ID27_DR.add(window.getID27_DR_k125());
            ID27_DR.add(window.getID27_DR_k150());
            ID27_DR.add(window.getID27_DR_k200());
            ID27_DR.add(window.getID27_DR_k250());
            ID27_DR.add(window.getID27_DR_k300());
            ID27_DR.add(window.getID27_DR_k400());
            ID27_DR.add(window.getID27_DR_k500());
        }

        for (String string : ID27_DR) {
            saveLdtFile.captureString(string);
            saveLdtFile.appendLdtSeparator();
        }

        int cPlaneSteps;
        int angleCplane;
        if(window.getID5_Dc().equalsIgnoreCase("")){
            cPlaneSteps = 360 / Integer.parseInt(properties.get_Property("ID5_Dc"));
            angleCplane = Integer.parseInt(properties.get_Property("ID5_Dc"));
        }else{
            cPlaneSteps = 360 / Integer.parseInt(window.getID5_Dc());
            angleCplane = Integer.parseInt(window.getID5_Dc());
        }
        for(int i = 0; i < cPlaneSteps; i++){
            saveLdtFile.captureString(String.valueOf(i * angleCplane));
            saveLdtFile.appendLdtSeparator();
        }

        int gammaSteps;
        int angleGamma;
        if(window.getID5_Dc().equalsIgnoreCase("")){
            gammaSteps = (180 / Integer.parseInt(properties.get_Property("ID7_Dg"))) + 1;
            angleGamma = Integer.parseInt(properties.get_Property("ID7_Dg"));
        }else{
            gammaSteps = (180 / Integer.parseInt(window.getID7_Dg())) + 1;
            angleGamma = Integer.parseInt(window.getID7_Dg());
        }
        for(int i = 0; i < gammaSteps; i++){
            saveLdtFile.captureString(String.valueOf(i * angleGamma));
            saveLdtFile.appendLdtSeparator();
        }

        System.out.println(values);
        System.out.println("Raw values: ");
        System.out.println(values.size());
        System.out.println("---------------------------------------------");

        System.out.println(createSetWithoutSymetry(values));
        System.out.println("createSetWithoutSymetry values: ");
        System.out.println(createSetWithoutSymetry(values).size());
        System.out.println("---------------------------------------------");

        System.out.println(createSetForVerticalSymetry(values));
        System.out.println("createSetForVerticalSymetry values: ");
        System.out.println(createSetForVerticalSymetry(values).size());
        System.out.println("---------------------------------------------");

        System.out.println(createSetC0_C180Symetry(values));
        System.out.println("createSetC0_C180Symetry values: ");
        System.out.println(createSetC0_C180Symetry(values).size());
        System.out.println("---------------------------------------------");

        System.out.println(createSetC90_C270Symetry(values));
        System.out.println("createSetC90_C270Symetry values: ");
        System.out.println(createSetC90_C270Symetry(values).size());
        System.out.println("---------------------------------------------");

        System.out.println(createSetC0_C180AndC90_C270Symetry(values));
        System.out.println("createSetC0_C180AndC90_C270Symetry values: ");
        System.out.println(createSetC0_C180AndC90_C270Symetry(values).size());
        System.out.println("---------------------------------------------");

        System.out.println(window.getIluminationForLDT());

        String symm;
        try {
            symm = window.getID3_symmetry();
        }catch (Exception e){
            symm = properties.get_Property("ID3_symmetry");
        }
        if(symm.equalsIgnoreCase(String.valueOf(0))){
            for (ArrayList<String> list : createSetWithoutSymetry(values)) {
                for (String string : list) {
                    if(window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(formatForEulumdatValues(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if (!window.getIluminationForLDT() && window.getCalculateFlux()) {
                        saveLdtFile.captureString(formatForEulumdatValuesCalFlux(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if(!window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(string);
                        saveLdtFile.appendLdtSeparator();
                    }
                }
            }
        }else if (symm.equalsIgnoreCase(String.valueOf(1))){
            ArrayList<String> val = createSetForVerticalSymetry(values);
            for (String string : val) {
                if(window.getIluminationForLDT() && !window.getCalculateFlux()){
                    saveLdtFile.captureString(formatForEulumdatValues(string));
                    saveLdtFile.appendLdtSeparator();
                }else if (!window.getIluminationForLDT() && window.getCalculateFlux()) {
                    saveLdtFile.captureString(formatForEulumdatValuesCalFlux(string));
                    saveLdtFile.appendLdtSeparator();
                }else if(!window.getIluminationForLDT() && !window.getCalculateFlux()){
                    saveLdtFile.captureString(string);
                    saveLdtFile.appendLdtSeparator();
                }

            }
        }else if (symm.equalsIgnoreCase(String.valueOf(2))){
            for (ArrayList<String> list : createSetC0_C180Symetry(values)) {
                for (String string : list) {
                    if(window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(formatForEulumdatValues(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if (!window.getIluminationForLDT() && window.getCalculateFlux()) {
                        saveLdtFile.captureString(formatForEulumdatValuesCalFlux(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if(!window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(string);
                        saveLdtFile.appendLdtSeparator();
                    }
                }
            }
        }else if (symm.equalsIgnoreCase(String.valueOf(3))){
            for (ArrayList<String> list : createSetC90_C270Symetry(values)) {
                for (String string : list) {
                    if(window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(formatForEulumdatValues(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if (!window.getIluminationForLDT() && window.getCalculateFlux()) {
                        saveLdtFile.captureString(formatForEulumdatValuesCalFlux(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if(!window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(string);
                        saveLdtFile.appendLdtSeparator();
                    }
                }
            }
        }else if(symm.equalsIgnoreCase(String.valueOf(4))){
            for (ArrayList<String> list : createSetC0_C180AndC90_C270Symetry(values)) {
                for (String string : list) {
                    if(window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(formatForEulumdatValues(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if (!window.getIluminationForLDT() && window.getCalculateFlux()) {
                        saveLdtFile.captureString(formatForEulumdatValuesCalFlux(string));
                        saveLdtFile.appendLdtSeparator();
                    }else if(!window.getIluminationForLDT() && !window.getCalculateFlux()){
                        saveLdtFile.captureString(string);
                        saveLdtFile.appendLdtSeparator();
                    }
                }
            }
        }
    }

    /*
    Funkcia separuje časť celkového záznamu dát, do takej podoby,
    ako sa bežne ukladá v podobe .ldt súbora
    -platí pre symetrické svietidlo podľa C0-C180
     */
    private ArrayList<ArrayList<String>> createSetC0_C180Symetry(ArrayList<ArrayList<String>> values) throws IOException {

        ArrayList<ArrayList<String>> setForMeasurementC0_C180Symetry = new ArrayList<>();

        int i = 0;
        int stepVal = Integer.parseInt(properties.get_Property("ID5_Dc"));
        for(ArrayList<String> strings: values){
            stepVal *= i;
            if(stepVal >= 0 && stepVal <= 180){
                setForMeasurementC0_C180Symetry.add(strings);
            }
            i++;
        }
        return setForMeasurementC0_C180Symetry;
    }

    /*
    Funkcia separuje časť celkového záznamu dát, do takej podoby,
    ako sa bežne ukladá v podobe .ldt súbora
    -platí pre symetrické svietidlo podľa C90-C270
     */
    private ArrayList<ArrayList<String>> createSetC90_C270Symetry(ArrayList<ArrayList<String>> values) throws IOException {

        ArrayList<ArrayList<String>> setForMeasurementC90_C270Symetry = new ArrayList<>();

        int i = 0;
        int stepVal = Integer.parseInt(properties.get_Property("ID5_Dc"));
        for(ArrayList<String> strings: values){
            stepVal *= i;
            if(stepVal >= 90 && stepVal <= 270){
                setForMeasurementC90_C270Symetry.add(strings);
            }
            i++;
        }
        return setForMeasurementC90_C270Symetry;
    }

    /*
    Funkcia separuje časť celkového záznamu dát, do takej podoby,
    ako sa bežne ukladá v podobe .ldt súbora
    -platí pre symetrické svietidlo podľa C0-C180 a C90-C270
     */
    private ArrayList<ArrayList<String>> createSetC0_C180AndC90_C270Symetry(ArrayList<ArrayList<String>> values) throws IOException {

        ArrayList<ArrayList<String>> setForMeasurementC0_C180AndC90_C270Symetry = new ArrayList<>();

        int i = 0;
        int stepVal = Integer.parseInt(properties.get_Property("ID5_Dc"));
        for(ArrayList<String> strings: values){
            stepVal *= i;
            if(stepVal >= 0 && stepVal <= 90){
                setForMeasurementC0_C180AndC90_C270Symetry.add(values.get(i));
            }
            i++;
        }

        return setForMeasurementC0_C180AndC90_C270Symetry;
    }

    /*
    Funkcia separuje časť celkového záznamu dát, do takej podoby,
    ako sa bežne ukladá v podobe .ldt súbora
    -platí pre nesymetrické svietidlo
     */
    private ArrayList<ArrayList<String>> createSetWithoutSymetry(ArrayList<ArrayList<String>> values){
        ArrayList<ArrayList<String>> setForMeasurementWithoutSymetry = new ArrayList<>(values);
        return setForMeasurementWithoutSymetry;
    }

    /*
    Funkcia separuje časť celkového záznamu dát, do takej podoby,
    ako sa bežne ukladá v podobe .ldt súbora
    -platí pre vertikálnu symetriu ...
     */
    private ArrayList<String> createSetForVerticalSymetry(ArrayList<ArrayList<String>> values) {
        ArrayList<String> setForMeasurementVerticalSymetry = new ArrayList<>(values.get(0));
        return setForMeasurementVerticalSymetry;
    }

    /*
    Prítomné funkcie upravujú skladbu nameraných dát ...
     */
    private ArrayList<String> editMeasurementValues(ArrayList<ArrayList<String>> measuremantStaticSetsForResultsWindow){
        ArrayList<String> editedMeasurementValues = new ArrayList<>();
        StringBuilder builder = new StringBuilder("");

        if(measuremantStaticSetsForResultsWindow != null){
            for (ArrayList<String> list: measuremantStaticSetsForResultsWindow){
                for (int i = 0; i < list.size(); i++){

                    if (i <= list.size()/2){
                        builder.append(list.get(i));
                        editedMeasurementValues.add(String.valueOf(builder));
                    }
                    if (i == list.size()/2){
                        editedMeasurementValues.add(String.valueOf("NEW"));
                    }
                    builder.delete(0, builder.length());
                }
            }

            for (ArrayList<String> list: measuremantStaticSetsForResultsWindow){
                for (int i = 0; i < list.size(); i++){

                    if (i >= list.size()/2){
                        builder.append(list.get(i));
                        editedMeasurementValues.add(String.valueOf(builder));
                    }
                    if (i == list.size() - 1){
                        editedMeasurementValues.add(String.valueOf("NEW"));
                    }
                    builder.delete(0, builder.length());
                }
            }
        }else{
            JOptionPane.showMessageDialog(this, "There are no measurement values ...");
        }
        return editedMeasurementValues;
    }

    private ArrayList<ArrayList<String>> editMeasurementValuesTo2DArrayList(ArrayList<String> list){

        ArrayList<ArrayList<String>> twoDArrayList = new ArrayList<>();
        ArrayList<String> help;
        ArrayList<String> toMove = new ArrayList<>();

        for (String string : list) {
            if(string.equalsIgnoreCase("NEW")){
                help = new ArrayList<>();
                help = (ArrayList<String>) toMove.clone();
                twoDArrayList.add(help);
                toMove.clear();
            }else{
                toMove.add(string);
            }
        }
        return reverseList(twoDArrayList);
    }

    private ArrayList<ArrayList<String>> reverseList(ArrayList<ArrayList<String>> arrayLists){

        for (int i = 0; i < arrayLists.size(); i++){
            if(i >= (arrayLists.size()/2)){
                Collections.reverse(arrayLists.get(i));
            }
        }
        return arrayLists;
    }

    private String formatForEulumdatValues(String actualValue){
        actualValue = String.valueOf(Double.parseDouble(actualValue) * (1000D/Double.valueOf(Window.getID26_lamps().get(0).ID26c_fluxOfLamp)));
        return actualValue;
    }

    private String formatForEulumdatValuesCalFlux(String actualValue){
        actualValue = String.valueOf(Double.parseDouble(actualValue) * (1000D/getFluxCalValue()));
        return actualValue;
    }

    private double getFluxCalValue(){
        return flux;
    }
}

