//
//  mh5000
//
//  Copyright (c) 2017 Michael Heras
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307,
//  USA.
//
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <chrono>
#include <thread>
#include </usr/include/Imlib2.h>
#include <vector>
#include "files.h"
#include "img.h"
#include "timers.h"
#include "options.h"

void sleepy_time()
{
    std::cout<<"starting timer"<<std::endl;
    bool flag = true;
    while (flag)
    {
        if (img.set_over_drive == 1)
        {
            over_drive();
            std::this_thread::sleep_for(std::chrono::seconds(img.time));
            //std::cout<<"Firing off in over drive timer"<<"\n";
        }
        else if (img.color_flag == 3 && img.time > 0 && img.mode == 0)
        {
             setup_images();
             set_colors();
             display_image();
             std::this_thread::sleep_for(std::chrono::seconds(img.time));
             //std::cout<<"Firing off in image setting with secial colors 3 timer"<<"\n";
        }
        else if (img.color_flag == 4 && img.time > 0 && img.mode == 0)
        {
			setup_images();
			set_colors();
			display_image();
			std::this_thread::sleep_for(std::chrono::seconds(img.time));
			//std::cout<<"Firing off in images colors 4 timer"<<"\n";
        }
		else
        {
            setup_images();
            set_colors();
            load_image();
            std::this_thread::sleep_for(std::chrono::seconds(img.time));
            //std::cout<<"Firing off in images settings and colors timer"<<"\n";
        }


    }


}

void set_initial_image_mode()
{
setup_images();
set_timer_colors();
load_image();
}
void over_drive()
{
img.mode = getRany(1,17);

setup_images();
set_timer_colors();
load_image();
}
void over_ride()
{

}
int set_random_range(float min, float max)
{
   return ((int)(min + max * rand() / RAND_MAX));
}
void set_default_time()
{
    if(img.time == 0)
        img.time = DEFAULT_TIME;
}

int set_over_drive_randoms(int n )
{
    return ( (rand() % n ) );
}


void set_initial_image()
{
img.mode = getRany(1,16);


setup_images();
set_timer_colors();
load_image();
}
void set_timer_colors(void)
{
    int i = 0;
    int c = ( (rand() % 4) );

//  std::cout<<"c= "<<c<<std::endl;

    switch(c)
    {
        case 0:
            set_one_random_color();
            break;
        case 1:
            set_two_random_colors();
            break;
        case 2:
            set_one_random_color();
            break;
        case 3:
            img.set_random_color_num = set_random_range(1.0,MAX_SHOW_COLORS);

        //  std::cout<<img.set_random_color_num<<std::endl;

            for( i = 0; i < img.set_random_color_num; i++)
                set_two_random_colors();
            break;
        default:
            set_timer_colors();
            break;

    }
}
