package mechanique;

import java.io.*; 
import java.util.*;
import java.io.Serializable;

public class RollShelf implements Printable, Commandline, Serializable {

    public RollShelf() { }
    private ArrayList<String> rolls = new ArrayList<String>(); 
    private String buffer = "";
    private Receiver receiver;
    private StoryController controller;
    private String sessionName = "";
    private String fileName = "";
    private String command = "";
    public Story story;
   // public boolean first;
    
    public String getRolls(String path)
    {
    
        File file = new File(path);
        String[] list = file.list();
        String dir = "<ul>";
        Boolean firstIteration = true;
        
        for(String listItem: list)
        {
            dir = dir.concat("<li><a href='mq?rollFileName=").concat(path).concat(listItem);
            dir = dir.concat("'>").concat(listItem).concat("</a></li>");
        }
        
        dir = dir.concat("</ul>");
        
        return dir;
    }
    
    //public String doRequest(String command, String fname, String sessionName, boolean start, Story s)
    public String doRequest(String command, String fname, Story s)
    {
        
       // this.sessionName = sessionName;
        this.fileName = fname;
        this.command = command;
        
        //Story s;
      //  this.first = true;
        
        if (s==null)
        {
            s = (Story) toolbox.deserialize(fname); 
            s.setDisplayUnit(this);
            this.story = s;
            s.run(true);
        }
        else
        {
            //s = (Story) toolbox.deserialize(sessionName+".roll");
            
            s.setDisplayUnit(this);
            this.buffer="";
            this.story = s;
            this.receiver = this.story.lastrc;
            this.receiver.setWait(true);
            this.receiver.receive(command,true);
          //  this.first=true;
            this.story.doStep();
        }
        
        
        return this.buffer;
    
    }
    
    public void setStory(StoryController s)
    {
        controller = s;
    }
    
    public void print(String t)
    {
        if (t.endsWith("-")) {
            this.buffer += " "+t.replaceAll("-$","");
        } else {
            this.buffer += "<br>"+t;
        }
        
    }
    
    public void input(Receiver r)
    {
     
        
        this.receiver = r;
        this.answer(this.command);
       
    }
    
    public void answer(String a)
    {
        if (a.equals("#save")) a="";
        if (a.equals("#mem")) a="";
        
        
      //  if (this.first){
       //     
      //      this.receiver.setWait(true);
      //      this.receiver.receive(a,true);
      //      this.first=false;
      //  }else{
            this.story.lastrc = this.receiver;
            //toolbox.serialize(this.sessionName+".roll",this.story);
            
       // }
        
        
        
        
    }
    
    public Story getStory()
    {
        return this.story;
    }
    
}
