//
//  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 <vector>
#include <algorithm>
#include </usr/include/Imlib2.h>
#include <pwd.h>
#include "options.h"
#include "img.h"
#include "files.h"
#include "timers.h"

int main(int argc, char **argv)
{
    imgdata.display = XOpenDisplay( NULL );

     if (!imgdata.display){
        std::cout<<"No display::Main"<<std::endl;
        exit(1);
    }
    init_options(argc, argv);
    check_options();

    switch (opts.which_file_type)
    {
        case 2:
            std::cout<<"Loading files ..."<<std::endl;
            loadFiles(opts.dfile, opts.pathORDir);
            std::cout<<"Done loading files."<<std::endl;
            if (opts.dfile.empty())
            {
                std::cout<<"Failed to load any files. "
                <<"check your directory/list for vaild files."<<std::endl;
                exit(EXIT_FAILURE);
            }
        break;
        case 3:
            std::cout<<"Loading files from list ..."<<std::endl;
            loadFiles_FromList(opts.dfile, opts.pathORDir);
            std::cout<<"Done loading files from list."<<std::endl;
            if (opts.dfile.empty())
            {
                std::cout<<"Failed to load any files. "
                <<"check your directory/list for vaild files."<<std::endl;
                exit(EXIT_FAILURE);
            }
        break;
        default:
        break;
    }
    setup_images();
   // std::cout<<"here 1"<<std::endl;
    set_colors();
  //  std::cout<<"here 2"<<std::endl;

    if ( img.set_ordered_list == 1 )
    {
        std::cout<<"Ordering List ..."<<std::endl;
        sort_V();
       std::cout<<"Ordering completed."<<std::endl;
    }
    if (img.set_randomize == 1)
    {
       std::cout<<"Randomizing  files ..."<<std::endl;
       randomizeFiles();
       std::cout<<"Files Now Randomized."<<std::endl;
    }


    if (img.set_over_drive == 1 ){
        set_default_time();
        sleepy_time();
    }

    if (img.time > 0 )
        sleepy_time();

    if ( (int)img.mode > 0 )
    { //std::cout<<"here 4"<<std::endl;
        load_image();
    }
    else
    {//std::cout<<"here 5"<<std::endl;
        display_image(); //for just colors
    }
   // std::cout<<"here 1"<<std::endl;
    XCloseDisplay(imgdata.display);

    return EXIT_SUCCESS;
}
