package de.wellenvogel.avnav.appapi;

import android.content.res.AssetManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import de.wellenvogel.avnav.util.AvnLog;

public class UserDirectoryRequestHandler extends DirectoryRequestHandler {
    private static String templateFiles[]=new String[]{"user.css","user.js"};
    public UserDirectoryRequestHandler(RequestHandler handler, IDeleteByUrl deleter) throws IOException {
        super(handler, RequestHandler.TYPE_USER, RequestHandler.typeDirs.get(RequestHandler.TYPE_USER).value, "user/viewer", deleter);
        AssetManager assets=handler.activity.getAssets();
        for (String filename : templateFiles){
            File file=new File(workDir,filename);
            if (! file.exists()){
                String templateName="viewer/"+filename;
                try {
                    InputStream src = assets.open(templateName);
                    AvnLog.i("creating user file " + filename + " from template");
                    FileOutputStream out=new FileOutputStream(file);
                    byte buffer[]=new byte[10000];
                    int rd=0;
                    while ((rd=src.read(buffer)) >=0 ){
                        out.write(buffer,0,rd);
                    }
                    out.close();
                    src.close();
                }catch (Throwable t){
                    AvnLog.e("unable to copy template "+templateName,t);
                }
            }
        }
    }
}

@@ -182,16 +182,44 @@
 tStream;
+%0Aimport java.io.PrintWriter;
 %0A%0Aimport
@@ -407,16 +407,100 @@
 r.js%22%7D;%0A
+    private static String emptyJsonFiles%5B%5D=new String%5B%5D%7B%22keys.json%22,%22images.json%22%7D;%0A
     publ
@@ -1638,20 +1638,542 @@
 %7D%0A        %7D%0A
+        for (String filename : emptyJsonFiles)%7B%0A            File file=new File(workDir,filename);%0A            if (! file.exists())%7B%0A                try %7B%0A                    AvnLog.i(%22creating empty user file %22 + filename );%0A                    PrintWriter out= new PrintWriter(new FileOutputStream(file));%0A                    out.println(%22%7B %7D%22);%0A                    out.close();%0A                %7Dcatch (Throwable t)%7B%0A                    AvnLog.e(%22unable to create %22+filename,t);%0A                %7D%0A            %7D%0A        %7D%0A
     %7D%0A%7D%0A

create empty json user files as needed