/*
 Copyright (c) 2007 Eric J. Feminella <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.filesystem
{
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.events.IOErrorEvent;
    import flash.events.Event;

    /**
     *
     * All static class which provides a mechanism for both synchronous
     * and asynchronous File creation on a local File system
     *
     */
    public final class FileWriter
    {
        /**
         *
         * Synchronously creates a new File to the local filesystem
         * and returns an open FileStream object. If the File exists
         * then the file is opened for update
         *
         * @param  the name of the file to create
         * @param  the path in which to create the file
         * @return an open FileStream intance referencing the File
         * @throws flash.errors.IOError
         *
         */
        public static function createSync(name:String, path:String) : FileStream
        {
            var file:File = new File();
            file.nativePath = path + File.separator + name;

            var stream:FileStream = new FileStream();

            if (!file.exists)
            {
                stream.open(file, FileMode.WRITE);
            }
            else
            {
                stream.open(file, FileMode.UPDATE);
            }
            return stream;
        }

        /**
         *
         * Asynchronously creates a new File to the local filesystem
         * and returns an open FileStream object. If the File exists
         * then the file is opened for update
         *
         * @param  the name of the file to create
         * @param  the path in which to create the file
         * @param  method in which to invoke upon file complete
         * @param  method in which to invoke upon file IOError
         * @return an open FileStream intance referencing the File
         * @throws flash.errors.IOError
         *
         */
        public static function createAsync(name:String, path:String, complete:Function, exception:Function) : FileStream
        {
            var file:File = new File();
            file.nativePath = path + File.separator + name;

            var stream:FileStream = new FileStream();
            stream.addEventListener(Event.COMPLETE, complete);
            stream.addEventListener(IOErrorEvent.IO_ERROR, exception);

            if (!file.exists)
            {
                stream.open(file, FileMode.WRITE);
            }
            else
            {
                stream.open(file, FileMode.UPDATE);
            }
            return stream
        }

        /**
         *
         * Determines if the current file exists
         *
         * @param  the name of the file
         * @param  the path in which the file resides
         * @return if the file being references currently exists
         *
         */
        public static function exists(name:String, path:String) : Boolean
        {
            var file:File = new File();
            file.nativePath = path + File.separator + name;

            return file.exists;
        }
    }
}