module File;
from Data import TutorialDB, PageDB;

 from Admin import UserId;
  from Options import Options;
  export FileSpace, File;

  object FileSpace is File*
    description: (*
        A FileSpace is an abstract model of a file space in the operating
        environment in which the CalendarTool is run.  The FileSpace is simply
        a collection of zero or more Files, with no other properties modeled
        here.
     *);
  end;

  object File is
    components: name:FileName and permissions:FilePermissions and
        file_type:FileType and data:FileData;
    description: (*
        A File is an abstraction of a file stored in the file space.  It has a
        name, permissions, type, and data.  These are the components sufficient
        to specify the behavior of Calendar Tool file operations.
    *);
  end File;

  object FileName is string
    description: (*
        The name of a file.  The string representation here is an abstraction
        of file names used in specific operating environments.  Implementations
        may obey any syntactic or semantic constraints imposed by a particular
        environment.
    *);
  end;

  object FilePermissions is is_readable:IsReadable and is_writable:IsWritable
    description: (*
        FilePermissions indicate whether a file is readable and/or writable.
    *);
  end;

  object IsReadable is boolean
    description: (*
        Flag indicating whether a file is readable, which is required to be
        true by the FileOpen operation.
    *);
  end;

  object IsWritable is boolean
    description: (*
        Flag indicating whether a file is writable, which is required to be
        true by the FileSave operation.
    *);
  end;

  object FileType is tutorial_type:TutorialType or page_type:PageType or other_type:OtherType
    description: (*
        The type of file data is either tutorial/page data (which we care about)
        or any other type of data that can be stored in a file, which we don't
        care about.
    *);
  end FileType;

  object TutorialType
    description: (*
        File data typing tag indicating that a file contains tutorial data
        created by CSTutor.
    *);
  end TutorialType;

  object PageType
    description: (*
        File data typing tag indicating that a file contains page data
        created by CSTutor.
    *);
  end PageType;

  object OtherType
    description: (*
        File data typing tag indicating that a file contains data other than
        tutorial/page data created by CSTutor.
    *);
  end OtherType;

  object FileData is TutorialRecord or PageRecord
    description: (*
        The abstract representation of FileData is a tutorial or page
        object. 
    *);
  end FileData;

  operation FileNewTutorial is
    inputs: uws:UserWorkSpace;
    outputs: uws':UserWorkSpace;

    description: (*
        Add a new empty tutorial to the workspace and make it current.
    *);

    precondition: ;

    postcondition:
        (exists (tr:TutorialRecord)
        		(tr in uws'.tutorials)
            (tr.authorID = uws.uid) and
            (tr.folderPathname = nil) and
            (uc.tutorial = nil));

  end FileNew;
	
	operation FileNewPage is 
			inputs: uw:UserWorkSpace;
			outputs: uw':UserWorkSpace;
	    description: (* Same as Tutorial *);
	end FileNewPage;
	
	operation FileNewPage is 
		inputs: uw:UserWorkSpace;
			outputs: uw':UserWorkSpace;
	    
	   description: (* Same as Tutorial *);
	end FileNewPage;
	
	operation FileNewQuiz is 
		inputs: uw:UserWorkSpace;
			outputs: uw':UserWorkSpace;
	    
		description: (* Same as Tutorial *);
	end FileNewQuiz;
	
	
	

  operation FileOpenTutorial is
    inputs: fs:FileSpace, fn:FileName, uws:UserWorkSpace;
    outputs: uws':UserWorkSpace;

    description: (*
        Open an existing tutorial file of the given name and put the data from
        that file in the workspace.
    *);

    precondition:
        (*
         * A file of the given name exists in the given file space, the file
         * is readable, and the file's data are of type tutorial.
         *)
        exists (file in fs)
            (file.name = fn) and
            file.permissions.is_readable and
            file.file_type?tutorial_type;

    postcondition:
        exists (tr:TutorialRecord)
            (tr in uws'.tutorials) and
            (uc.uid = uws.uid) and
            (exists (file in fs)
                (file.name = fn) and
                (uc = file.data));

end FileOpenTutorial;

  operation FileClose is
  
   inputs: fs:FileSpace and uw:UserWorkSpace;
  outputs: fs':FileSpace and uw':UserWorkSpace;


  	description: (*
        Close the current window 
    *);
end FileOpenTutorial;

operation FileSave is 
  inputs: fs:FileSpace and uw:UserWorkSpace;
  outputs: fs':FileSpace and uw':UserWorkSpace;
    description: (*
        Save the current document into the filespace
    *);
  end FileSave;

end File;