/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bg.alpha.base.sample.zcp;

import bg.alpha.base.*;
//import bg.alpha.base.MyFrame;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import java.awt.*;

/**
 *
 * @author z
 */
public class Main {

    static void importElements(long lId, ZCP zcp, String sDir, String sFile) {
        File f = new File(sDir+(sFile.equals("")?"":(java.io.File.separator
            +sFile)));
        if(f.exists()) {
            try {
                if(f.isDirectory()) {
                    System.out.println("#1 "+sDir+java.io.File.separator+sFile);
                    if(!sFile.equals("")) {
                        zcp.addElement(lId, sFile);
                        zcp.moveToSubSet(lId);
                    }
                    String[] children = f.list();
                    if(children!=null) {
                        for(int i=0, n=children.length; i<n; i++) {
                            importElements(lId, zcp, sDir+(sFile.equals("")?""
                                :(java.io.File.separator+sFile)), children[i]);
                        }
                    }
                    if(!sFile.equals("")) {
                        zcp.moveToSuperSet(lId);
                    }
                } else if(f.isFile()) {
                    try {
                        RandomAccessFile raf = new RandomAccessFile(f,"r");
                        byte bE[] = new byte[(int)raf.length()];
                        if(bE!=null) {
                            raf.read(bE);
                            if(!sFile.equals("")) {
                                zcp.addElement(lId, sFile);
                                zcp.moveToSubSet(lId);
                                zcp.addElement(lId, bE, 0, bE.length);
                                zcp.moveToSuperSet(lId);
                                //String sE = new String(bE,"UTF-8");
                                //zcp.addElement(lId, sE);
                            }
                        }
                    } catch (java.io.FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (java.io.IOException ex) {
                        ex.printStackTrace();
                    }
                }
            } catch (ZZZException ex) {
                ex.printStackTrace();
            }
        }
    }

    // Recursive print elements:
    static void printElements(long lId, ZCP zcp, String spaces) throws
        ZZZException {
        if(zcp.moveToFirstElement(lId)) {
            do {
                System.out.println(spaces+zcp.getElement(lId));
                zcp.moveToSubSet(lId);
                printElements(lId, zcp, spaces+".");
                zcp.moveToSuperSet(lId);
            } while(zcp.moveToNextElement(lId));
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            File f = new File("\\testzcp1.zzz");
            if(f.exists()) {
                f.delete();
            }
            f = new File("\\testzcp2.zzz");
            if(f.exists()) {
                f.delete();
            }
            f = new File("\\testzcp1.zzz.sw");
            if(f.exists()) {
                f.delete();
            }
            // Create Alpha Base Object
            ZCP zcp = new ZCP("\\testzcp1.zzz","\\testzcp2.zzz",
                ZZZDefs.cZCO_IN_FILE);

            // Begin write process with max timeout:
            long lId = zcp.BeginWriteProcess(Long.MAX_VALUE);
            try {
                importElements(lId, zcp, "\\testzcp1.zzz.import", "");
                //zcp.addElement(lId, "main");
                //zcp.addElement(lId, "NotebookAddData");
                //zcp.addElement(lId, "NotebookOpening");

                zcp.addElement(lId, "apple");
                zcp.moveToSubSet(lId);
                 zcp.addElement(lId, "green");
                 zcp.addElement(lId, "tasty");
                 zcp.moveToSuperSet(lId);

                zcp.addElement(lId, "orange");
                zcp.moveToSubSet(lId);
                 zcp.addElement(lId, "orange");
                 zcp.addElement(lId, "tasty");
                 zcp.moveToSuperSet(lId);

                printElements(lId, zcp, "");

                /*long lId_w3 = zcp.BeginWriteProcess(1000);
                try {
                    zcp.addElement(lId_w3, "banana3");
                    zcp.moveToSubSet(lId_w3);
                     zcp.addElement(lId_w3, "yellow3");
                    //zcp.BreakWriteProcess();
                     zcp.addElement(lId_w3, "tasty3");
                     zcp.moveToSuperSet(lId_w3);
                    System.out.println("lId_w3:"+zcp.getElement(lId_w3));
                } finally {
                    zcp.EndProcess(lId_w3);
                }*/
            } finally {
                zcp.EndProcess(lId);
            }
            // Begin write process with 1000 ms or 1 s timeout:
            long lId_w2 = zcp.BeginWriteProcess(1000);
            try {
                zcp.addElement(lId_w2, "banana");
                zcp.moveToSubSet(lId_w2);
                 zcp.addElement(lId_w2, "yellow");
                 //zcp.BreakWriteProcess();
                 zcp.addElement(lId_w2, "tasty");
                 zcp.moveToSuperSet(lId_w2);
                System.out.println("lId_w2:"+zcp.getElement(lId_w2));
            } finally {
                zcp.EndProcess(lId_w2);
            }
            // Begin of two read processes:
            long lId_r1 = zcp.BeginReadProcess();
            long lId_r2 = zcp.BeginReadProcess();
            try {
                //System.out.println("lId_r1");
                printElements(lId_r1, zcp, "");
                //System.out.println("lId_r2");
                printElements(lId_r2, zcp, "");
                zcp.moveToElement(lId_r1, "apple");
                zcp.moveToElement(lId_r2, "orange");
                System.out.println("lId_r1:"+zcp.getElement(lId_r1));
                System.out.println("lId_r2:"+zcp.getElement(lId_r2));
            } finally {
                zcp.EndProcess(lId_r1);
                zcp.EndProcess(lId_r2);
            }
            zcp.ZZZClose();
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
        System.out.println("-----");
        System.out.println("v."+ProgramVersion.iMajor+"."
            +ProgramVersion.iMinor+"."
            +ProgramVersion.iRelease+"."
            +ProgramVersion.iBuild+".");

        /*MyFrame frame = new MyFrame();
        frame.setSize(new Dimension(170, 100));
        frame.setVisible(true);
        try {
            ZC66 zc = new ZC66("\\testzcp1.zzz", ZZZDefs.cZCO_IN_FILE);
            frame.SetZC(zc);
            zc=null;
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }*/
    }
}
