//[skeleton1.
package visual.statik.sampled;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Arrays;

import io.ResourceFinder;

/**
 * A utility class for constructing/creating 
 * visual.statik.sampled.Content objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ContentFactory
{
    private ImageFactory          imageFactory;
    
    private static final int      DEFAULT_CHANNELS = 3;
    private static final boolean  ROTATABLE        = true;    

    /**
     * Default Constructor
     */
    public ContentFactory()
    {
       super();       
       imageFactory = new ImageFactory();
    }
    

    /**
     * Create a Content from a BufferedImage
     *
     * @param image     The BufferedImage
     * @param rotatable false to prevent rotations (and improve performance)
     * @return          The Content
     */
    public Content createContent(BufferedImage image,
                                 boolean rotatable)
    {
       return new Content(image, 0, 0, rotatable);       
    }
    

    /**
     * Create a Content from a BufferedImage
     *
     * @param image     The BufferedImage
     * @return          The Content
     */
    public Content createContent(BufferedImage image)
    {
       return new Content(image, 0, 0, ROTATABLE);       
    }



    /**
     * Create a Content from an Image
     *
     * @param image      The original Image
     * @param channels   3 for RGB; 4 for ARGB
     * @param rotatable  false to prevent rotations (and improve performance)
     * @return           The Content
     */
    public Content createContent(Image   image,
                                 int     channels,
                                 boolean rotatable)
    {
       BufferedImage       bi;

       bi = imageFactory.createBufferedImage(image, channels);
       return createContent(bi, rotatable);
    }


    /**
     * Create a Content from an Image
     *
     * @param image      The original Image
     * @param channels   3 for RGB; 4 for ARGB
     * @return           The Content
     */
    public Content createContent(Image image,
                                 int   channels)
    {
       return createContent(image, channels, ROTATABLE);
    }


    /**
     * Create a Content (with the default number
     * of channels) from an Image
     *
     * @param image      The original Image
     * @param rotatable  false to prevent rotations (and improve performance)
     * @return           The Content
     */
    public Content createContent(Image image,
                                 boolean rotatable)
    {
       return createContent(image, DEFAULT_CHANNELS, 
                            rotatable);
    }



    /**
     * Create a Content (with the default number
     * of channels) from an Image
     *
     * @param image      The original Image
     * @return           The Content
     */
    public Content createContent(Image image)
    {
       return createContent(image, 
                            DEFAULT_CHANNELS, 
                            ROTATABLE);
    }



    /**
     * Create a Content from a file/resource
     * containing an Image
     *
     * @param name       The name of the file/resource
     * @param channels   3 for RGB; 4 for ARGB
     * @param rotatable  false to prevent rotations (and improve performance)
     * @return           The Content
     */
    public Content createContent(String name,
                                 int channels,
                                 boolean rotatable)
    {
       BufferedImage          bi;
       
       bi = imageFactory.createBufferedImage(name, channels);
       return createContent(bi, rotatable);
    }



    /**
     * Create a Content from a file/resource
     * containing an Image
     *
     * @param name       The name of the file
     * @param channels   3 for RGB; 4 for ARGB
     * @return           The Content
     */
    public Content createContent(String name,
                                 int channels)
    {
       return createContent(name,channels, ROTATABLE);
    }


    /**
     * Create a Content (with the default number of
     * channels) from a file containing an Image
     *
     * @param name       The name of the file
     * @param rotatable  false to prevent rotations (and improve performance)
     * @return           The Content
     */
    public Content createContent(String name, 
                                 boolean rotatable)
    {
       return createContent(name, 
                            DEFAULT_CHANNELS, 
                            rotatable);
    }


    /**
     * Create a Content (with the default number of
     * channels) from a file/resource containing an Image
     *
     * @param name       The name of the file
     * @return           The Content
     */
    public Content createContent(String name)
    {
       return createContent(name, 
                            DEFAULT_CHANNELS, 
                            ROTATABLE);
    }

//]skeleton1.


    /**
     * Create an array of Content objects 
     * from an array of images in files/resources
     *
     * @param name     The names of the file/resource
     * @param channels 3 for RGB, 4 for ARGB
     * @return         The Content objects
     */
    public Content[] createContents(String[] names,
                                    int    channels)    
    {
       BufferedImage[]   images;
       Content[]         result;
       int               n;
       

       n      = names.length;       
       images = imageFactory.createBufferedImages(names, 
                                                  channels);
       result = new Content[n];        

       for (int i=0; i<n; i++)
       {
          result[i] = createContent(images[i], ROTATABLE);
       }
        
       return result;
    }    


//[filter.

    /**
     * Create an array of Content objects from a 
     * group of files/resources containing images
     *
     * @param path        The path to the directory containing the images
     * @param filter      The FilenameFilter to use
     * @param channels    3 for RGB; 4 for ARGB
     * @return            The array of Content objects
     */
    public Content[] createContents(String         path,
                                    FilenameFilter filter,
                                    int            channels)
    {
       File            dir;
       int             length;       
       Content[]       rbi;       
       String[]        fileNames;

       
       dir = new File(path);

       fileNames = dir.list(filter);
       Arrays.sort(fileNames);

       length = fileNames.length;
       rbi = new Content[length];

       for (int i=0; i < length; i++)
       {
          rbi[i] = createContent(fileNames[i], 
                                 channels, 
                                 ROTATABLE);
       }
       return rbi;       
    }


    /**
     * Create an array Content (with the default
     * number of channels) from a group of files containing images
     *
     * @param path    The path to the directory containing the images
     * @param filter  The FilenameFilter to use
     * @return        The array of Content objects
     */
    public Content[] createContents(String path,
                                    FilenameFilter filter)
    {
       return createContents(path, filter, DEFAULT_CHANNELS);
    }
//]filter.
//[skeleton2.   

    /**
     * Create an array of Content objects 
     * from an "array" of images in a file
     *
     * @param name     The name of the file/resource
     * @param n        The number of images
     * @param channels 3 for RGB, 4 for ARGB
     * @return         The Content objects or null if an Exception was thrown
     */
    public Content[] createContents(String name,
                                    int    n, 
                                    int    channels)    
    {
       BufferedImage[]   images;
       Content[]         result;
        

       images = imageFactory.createBufferedImages(name, n, 
                                                  channels);
        
       result = new Content[n];        
       for (int i=0; i<n; i++)
       {
          result[i] = createContent(images[i], ROTATABLE);
       }
        
       return result;
    }    



    /**
     * Create an array of Content objects 
     * from a table-oriented Image in a file
     *
     * @param name     The name of the file/resource
     * @param rows     The number of rows
     * @param columns  The number of columns
     * @param channels 3 for RGB, 4 for ARGB
     * @return         The Content objects or null if an Exception was thrown
     */
    public Content[][] createContents(String name, 
                                      int rows, 
                                      int columns, 
                                      int channels)
    {
       BufferedImage[][]   images;
       Content[][]         result;
        

       images = imageFactory.createBufferedImages(name, 
                                                  rows, 
                                                  columns, 
                                                  channels);
        
       result = new Content[rows][columns];        
       for (int r=0; r<rows; r++)
       {
          for (int c=0; c<columns; c++)
          {
             result[r][c] = createContent(images[r][c], 
                                          ROTATABLE);
          }
       }
        
       return result;
    }    

}
//]skeleton2.   
