/*=============================================================================
Exemple SDL
===============================================================================
Preprocesseur
=============================================================================*/
#include "SDL/SDL.h"
/*=============================================================================
programme principal
=============================================================================*/

#include "fractal.hpp"

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char * argv[])
{
    ////chessy loading file
    //if (argc != 2)
    //{
    //    cout << "invalid argument\n";
    //    return 0;
    //}
    //ifstream test_file(argv[1]);
    //string line,buff;
    //do
    //{
    //    getline(test_file,buff);
    //    line.append(buff);
    //}while(test_file);
    //cout << line << endl;
	string line = "{o:a[+ao]-ao;}ao";

    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen = SDL_SetVideoMode(600, 400, 8, SDL_SWSURFACE);
    SDL_Event event;

    int keypress = 0,
        mx = 0,
        my = 0;
        
    Uint32 brown = SDL_MapRGB( screen->format, 0x66, 0x33, 0x00 ),
           red = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 ),
           green = SDL_MapRGB( screen->format, 0x00, 0xFF, 0x00 );
           
    fractal arbre(line,300,300);
    arbre.grow();
    arbre.grow();
    arbre.grow();
    arbre.print();

    while(!keypress)
    {
        SDL_Flip(screen);
        SDL_FillRect(screen,0,0);

        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    keypress = 1;
                    break;
                case SDL_KEYDOWN:
                    keypress = 1;
                    break;
                case SDL_MOUSEMOTION:
                    mx = event.motion.x;
                    my = event.motion.y;
                    break;
                case SDL_MOUSEBUTTONDOWN:
                    if (arbre.getTouch() == 'o')
                        arbre.grow();
                    else if (arbre.getTouch()=='a')
                        arbre.trimTouch();
                    arbre.print();
                    break;
            }
        }
        
        arbre.reset();
        while (!arbre.end())
        {
            arbre.parse();
            Uint32 color = 0;
            SDL_Rect tmp = {arbre.position().x(), arbre.position().y(), 0, 0};
            if (arbre.get()=='o')
            {
                tmp.h = 20;
                tmp.w = 20;
                color = green;
            }
            else if (arbre.get()=='a')
            {
                tmp.h = 7;
                tmp.w = 7;
                color = brown;
            }
            
            if (color)
            {
                if (tmp.x<mx && tmp.x+tmp.w>mx && tmp.y<my && tmp.y+tmp.h>my)
                {
                    arbre.touch();
                    color = red;
                }
                SDL_FillRect(screen,&tmp,color);
            }
            
            arbre.next();
        }
    }
    SDL_Quit();
    return 0;
}
