#include "themes.hpp"

#include "common.hpp"
#include "config.hpp"
#include "theme.hpp"
#include "gettext.hpp"

#include <fstream>

using conv::atoi;
using std::string;

Themes::Themes() 
  : resolution_dependant_background_needed(false)
{}

Themes* Themes::instance = 0;

Themes* Themes::get_instance() 
{
  if (instance == 0) {
    instance = new Themes;
  }
  return instance;
}

string Themes::resolution_dependant_background(const string& bg)
{
  if (!resolution_dependant_background_needed)
    return bg;

  int i = bg.rfind('.');

  if (i != string::npos) {

    Config *config = Config::get_instance();

    string name = bg.substr(0, i);
    string ext = bg.substr(i, bg.size()-i);

    if ((config->p_v_res()/16)/(config->p_h_res()/10) == 1) 
	return name + "-16-10" + ext;
    else if ((config->p_v_res()/16)/(config->p_h_res()/9) == 1) 
      return name + "-16-9" + ext;
    else if ((config->p_v_res()/4)/(config->p_h_res()/3) == 1)
      return name + "-4-3" + ext;
    else {
      std::cerr << "WARNING: unsupported resolution, using 4:3 background" << std::endl;
      return name + "-4-3" + ext;
    }
  }

  std::cerr << "ERROR: can't find . in " << bg << std::endl;
  return "";
}


bool Themes::parser(const string& cur_theme, bool startup)
{
  if (cur_theme == "")
    return false;

  Config *config = Config::get_instance();

  string theme_def = prefix"/share/mms/themes/" + cur_theme + "/theme.conf";

  if (config->p_homedir() != "/etc/mms/")
    theme_def = config->p_homedir() + "themes/" + cur_theme + "/theme.conf";

  std::ifstream in;

  in.open(theme_def.c_str(), std::ios::in);
  
  if (!in.is_open()) {
    if (startup) {
      DebugPrint perror(gettext("Could not open theme") + string(" ") + 
			theme_def, Print::DEBUGGING, DebugPrint::CRITICAL, 
			"THEME");
      exit(1);
    } else {
      Print pdialog(gettext("Could not open theme"), Print::SCREEN);
      return false;
    }
  } else {
   
    string name, value, str;
    
    for(int line=1; getline(in, str); line++) {

      string::size_type p0 = str.find_first_not_of("\t ");
      if ((p0 > str.size()) || (str[p0] == '#')) 
	continue;
      
      string::size_type p = str.find("=");
      
      if (p == string::npos) {
	DebugPrint perror(gettext("Illegal line in theme file, missing '=' on line ") + 
			  conv::itos(line),
			  Print::DEBUGGING, DebugPrint::WARNING, "THEME");
	continue;
      }
    
      name = str.substr(0,p);
      value = str.substr(p+1);

      name = string_format::trim(name);
      value = string_format::trim(value);

      if (name == "version") {
	if (value != MMS_THEME_VERSION) {
	  if (startup) {
            DebugPrint perror(gettext("Unsupported theme version, got version ") + value + gettext(" expected ") + MMS_THEME_VERSION,
                              Print::DEBUGGING, DebugPrint::CRITICAL, "THEME");
	    exit(1);
	  } else {

	    Print pdialog(gettext("Unsupported theme version"), Print::SCREEN);

	    in.close();

	    return false;
	  }
	}
      }

      if (name == "show_header") {
	if (value == "yes")
	  show_header = true;
	else
	  show_header = false;
      }

      if (name == "resolution_dependant_background") {
	if (value == "yes")
	  resolution_dependant_background_needed = true;
	else
	  resolution_dependant_background_needed = false;
      }

      if (name == "general.rectangle_color") {
	general_rectangle_color1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_rectangle_color2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_rectangle_color3 = atoi(value);
	value = value.substr(value.find(",")+1);
	general_rectangle_color4 = atoi(value);
      } else if (name == "general.marked_picture") {
	general_marked_picture_color1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_marked_picture_color2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_marked_picture_color3 = atoi(value);
	value = value.substr(value.find(",")+1);
	general_marked_picture_color4 = atoi(value);
      } else if (name == "general.search") {
	general_search = value;
      } else if (name == "general.search_rectangle_color") {
	general_search_rectangle_color1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_search_rectangle_color2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	general_search_rectangle_color3 = atoi(value);
	value = value.substr(value.find(",")+1);
	general_search_rectangle_color4 = atoi(value);
      } else if (name == "startmenu.font") {
	startmenu_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	startmenu_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	startmenu_font3 = atoi(value);
      } else if (name == "startmenu.background") {
	startmenu_background = resolution_dependant_background(value);
      } else if (name == "startmenu.music_dir") {
	startmenu_music_dir = value;
      } else if (name == "startmenu.music_cd") {
	startmenu_music_cd = value;
      } else if (name == "startmenu.radio") {
	startmenu_radio = value;
      } else if (name == "startmenu.movie_dir") {
	startmenu_movie_dir = value;
      } else if (name == "startmenu.movie_cd") {
	startmenu_movie_cd = value;
      } else if (name == "startmenu.pictures_dir") {
	startmenu_pictures_dir = value;
      } else if (name == "startmenu.pictures_cd") {
	startmenu_pictures_cd = value;
	  } else if (name == "startmenu.game_dir") {
	startmenu_game_dir = value;
      } else if (name == "startmenu.game_cd") {
	startmenu_game_cd = value;
      } else if (name == "startmenu.epg") {
	startmenu_epg = value;
      } else if (name == "startmenu.tv") {
	startmenu_tv = value;
      } else if (name == "startmenu.open_close") {
	startmenu_open_close = value;
      } else if (name == "startmenu.options") {
	startmenu_options = value;
      } else if (name == "startmenu.themes") {
	startmenu_themes = value;
      } else if (name == "startmenu.quit") {
	startmenu_quit = value;
      } else if (name == "audio.playback_font") {
	audio_playback_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_playback_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_playback_font3 = atoi(value);
      } else if (name == "audio.font") {
	audio_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_font3 = atoi(value);
      } else if (name == "audio.marked_font") {
	audio_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_marked_font3 = atoi(value);
      } else if (name == "audio.header_font") {
	audio_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	audio_header_font3 = atoi(value);
      } else if (name == "audio.background") {
	audio_background = resolution_dependant_background(value);
      } else if (name == "audio.dir") {
	audio_dir = value;
      } else if (name == "audio.dir_marked") {
	audio_dir_marked = value;
      } else if (name == "audio.marked") {
	audio_marked = value;
      } else if (name == "audio.marked_left") {
	audio_marked_left = value;
      } else if (name == "audio.marked_right") {
	audio_marked_right = value;
      } else if (name == "audio.marked_small") {
	audio_marked_small = value;
      } else if (name == "audio.marked_small_left") {
	audio_marked_small_left = value;
      } else if (name == "audio.marked_small_right") {
	audio_marked_small_right = value;
      } else if (name == "audio.playlist_icon") {
	audio_playlist_icon = value;
      } else if (name == "audio.playback_pause") {
	audio_playback_pause = value;
      } else if (name == "audio.playback_play") {
	audio_playback_play = value;
      } else if (name == "audio.mute_icon") {
 	audio_mute = value;
      } else if (name == "audio.repeat_icon") {
 	audio_repeat = value;
 	  } else if (name == "audio.shutdown_icon") {
 	audio_shutdown = value;
      } else if (name == "audio.volume_icon") {
 	audio_volume = value;
      } else if (name == "audio.volume_icon_fg") {
 	audio_volume_bar = value;
      } else if (name == "audio.random_icon") {
 	audio_random = value;
      } else if (name == "audio.randomi_icon") {
 	audio_randomi = value;
      } else if (name == "audio.randomw_icon") {
 	audio_randomw = value;
      } else if (name == "audio.file") {
 	audio_file = value;
      } else if (name == "movie.font") {
	movie_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_font3 = atoi(value);
      } else if (name == "movie.marked_font") {
	movie_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_marked_font3 = atoi(value);
      } else if (name == "movie.header_font") {
	movie_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	movie_header_font3 = atoi(value);
      } else if (name == "movie.background") {
	movie_background = resolution_dependant_background(value);
      } else if (name == "movie.dir") {
	movie_dir = value;
      } else if (name == "movie.dir_marked") {
	movie_dir_marked = value;
      } else if (name == "movie.marked") {
	movie_marked = value;
      } else if (name == "movie.marked_left") {
	movie_marked_left = value;
      } else if (name == "movie.marked_right") {
	movie_marked_right = value;
      } else if (name == "movie.missing_cover") {
	movie_mcover = value;
      } else if (name == "movie.missing_cover_small") {
	movie_mcover_small = value;
      } else if (name == "movie.rating_icon") {
	movie_rating_icon = value;
      } else if (name == "movie.rating_icon_half") {
	movie_rating_icon_half = value;
      } else if (name == "pictures.font") {
	pictures_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_font3 = atoi(value);
      } else if (name == "pictures.marked_font") {
	pictures_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_marked_font3 = atoi(value);
      } else if (name == "pictures.header_font") {
	pictures_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	pictures_header_font3 = atoi(value);
      } else if (name == "pictures.background") {
	pictures_background = resolution_dependant_background(value);
      } else if (name == "pictures.dir") {
	pictures_dir = value;
      } else if (name == "pictures.dir_marked") {
	pictures_dir_marked = value;
      } else if (name == "epg.font") {
	epg_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_font3 = atoi(value);
	  } else if (name == "game.font") {
	game_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_font3 = atoi(value);
      } else if (name == "game.marked_font") {
	game_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_marked_font3 = atoi(value);
      } else if (name == "game.header_font") {
	game_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	game_header_font3 = atoi(value);
      } else if (name == "game.background") {
	game_background = resolution_dependant_background(value);
      } else if (name == "game.dir") {
	game_dir = value;
      } else if (name == "game.dir_marked") {
	game_dir_marked = value;
      } else if (name == "game.marked") {
	game_marked = value;
      } else if (name == "game.marked_left") {
	game_marked_left = value;
      } else if (name == "game.marked_right") {
	game_marked_right = value;
      } else if (name == "game.missing_cover") {
	game_mcover = value;
      } else if (name == "game.rating_icon") {
	game_rating_icon = value;
      } else if (name == "game.rating_icon_half") {
	game_rating_icon_half = value;
      } else if (name == "epg.header_font") {
	epg_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_header_font3 = atoi(value);
      } else if (name == "epg.time_bar") {
	epg_time_bar1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_time_bar2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_time_bar3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_time_bar4 = atoi(value);
      } else if (name == "epg.bar") {
	epg_bar1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_bar2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_bar3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_bar4 = atoi(value);
      } else if (name == "epg.marked") {
	epg_marked1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_marked2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_marked3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_marked4 = atoi(value);
      } else if (name == "epg.marked_font") {
	epg_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_marked_font3 = atoi(value);
      } else if (name == "epg.boxes") {
	epg_boxes1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_boxes2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_boxes3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_boxes4 = atoi(value);
      } else if (name == "epg.boxes_font") {
	epg_boxes_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_boxes_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	epg_boxes_font3 = atoi(value);
      } else if (name == "epg.background") {
	epg_background = resolution_dependant_background(value);
      } else if (name == "dialog.font") {
	dialog_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_font3 = atoi(value);
      } else if (name == "dialog.background") {
	dialog_background1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_background2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_background3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_background4 = atoi(value);
      } else if (name == "dialog.marked") {
	dialog_marked1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_marked2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_marked3 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	dialog_marked4 = atoi(value);
      } else if (name == "options.font") {
	options_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	options_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	options_font3 = atoi(value);
      } else if (name == "options.header_font") {
	options_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	options_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	options_header_font3 = atoi(value);
      } else if (name == "options.background") {
	options_background = resolution_dependant_background(value);
      } else if (name == "options.marked") {
	options_marked = value;
      } else if (name == "options.marked_left") {
	options_marked_left = value;
      } else if (name == "options.marked_right") {
	options_marked_right = value;
      } else if (name == "themes.font") {
	themes_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_font3 = atoi(value);
      } else if (name == "themes.marked_font") {
	themes_marked_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_marked_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_marked_font3 = atoi(value);
      } else if (name == "themes.header_font") {
	themes_header_font1 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_header_font2 = atoi(value.substr(0,value.find(",")));
	value = value.substr(value.find(",")+1);
	themes_header_font3 = atoi(value);
      } else if (name == "themes.background") {
	themes_background = resolution_dependant_background(value);
      } else if (name == "themes.marked") {
	themes_marked = value;
      } else if (name == "themes.marked_left") {
	themes_marked_left = value;
      } else if (name == "themes.marked_right") {
	themes_marked_right = value;
      } else if (name == "progressbar.bar") {
	progressbar_bar = value;
      }
    }
    in.close();
  }

  return true;
}
