/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MineSweeper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;

/**
 *
 * @author Cecile
 */
public class GameTypeMGR {
    public static void help()
    {
        System.out.println("GameTypeMGR : is a Game type manager, it's to make the");
        System.out.println("distinction between different kind of game giving them");
        System.out.println("names that allow to identify them");
        System.out.println("To use them in another project you only need to create");
        System.out.println("a new GameType implementing class");
        System.out.println("The class will be loaded using the ClassForName instruction");
        System.out.println("Usage : GameTypeMGR GameName [-i GameTypeFile] [show|[add|del|mod] gamestr]");
        System.out.println("to get the format simply type GameTypeMGR GameName format");
        System.out.println("Modify will delete and add another instance of the object");
    }
    public static void main(String []args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException
    {
        Class cl=null;
       
        args=new String[3];
        args[0]="MineSweeper";
        args[1]="del";
        args[2]="toto";//10,6,5
        /*
        args=new String[3];
        args[0]="MineSweeper";
        args[1]="show";
         */
        if(args.length>=1 && (args[0].equals("-h") || args[0].equals("--help")))
        {
            help();
            System.exit(0);
        }
        if(args.length<2)
        {
            System.err.println("You need at least to specify the GameName and the action");
            System.exit(1);
        }
        String classname=args[0]+"."+args[0]+"GameType";
        try
        {
            cl = Class.forName(classname);

        }catch(ClassNotFoundException cnfe)
        {
            System.err.println("Could not find the appropriate class : "+classname);
            System.exit(1);
        }

        if (cl!=null && GameType.class.isAssignableFrom(cl)) {
            System.out.println("Loading the good class");
        } else {
            System.err.println("**Not assignable for");
        }
        String input_file_s=null;
        int nextarg=1;
        if(!args[1].equals("-i"))
        {
            input_file_s=args[0]+"_GamesTypes.os";
        }
        else if(args.length>2)
        {
            input_file_s=args[2];
            nextarg=3;
        }
        if(input_file_s==null)
        {
            System.out.println("You need to specify a filename");
        }
        File input_file=new File(input_file_s);
        GameTypes gts=null;
        if(input_file.isFile())
        {
            FileInputStream fis = new FileInputStream(input_file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            gts=(GameTypes)ois.readObject();
            ois.close();
        }
        else if(!input_file.exists())
        {
            FileOutputStream fos = new FileOutputStream(input_file);
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            gts = new GameTypes();
            oos.writeObject(gts);
            oos.close();
        }
        if(gts==null)
        {
            System.err.println("Could not open the game type file");
            System.exit(1);
        }
        if(args[nextarg].equals("show"))
        {
            Iterator<GameType> it=gts.iterator();
            while(it.hasNext())
            {
                System.out.println(it.next().toString());
            }
        }
        else if(args[nextarg].equals("add") && (nextarg+1)<args.length)
        {
            GameType gt=(GameType)cl.newInstance();
            gt.initialize(args[nextarg+1]);
            gts.add(cl.cast(gt));
        }
        else if(args[nextarg].equals("del") && (nextarg+1)<args.length)
        {
            GameType gt=(GameType)cl.newInstance();
            gt.initialize(args[nextarg+1]);
            gts.remove(gt);
        }
        else if(args[nextarg].equals("mod") && (nextarg+1)<args.length )
        {
            GameType gt=(GameType)cl.newInstance();
            gt.initialize(args[nextarg+1]);
            gts.remove(gt);
            gts.add(gt);
        }
        else
        {
            System.err.println("Bad arguments");
            System.exit(1);
        }


        FileOutputStream fos = new FileOutputStream(input_file);
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(gts);
        oos.close();
        /*
        GameType gt=(GameType)cl.newInstance();
        gt.initialize("toto:10:20:15");
        System.out.println(gt.toString()+"\n");
        cl.cast(gt);
         */
    }
}

