#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

#######################################################################
#
#    MyMetrix for VU+
#    Coded by iMaxxx (c) 2013
#    Support: www.vuplus-support.com
#
#  This plugin is licensed under the Creative Commons
#  Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#  To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
#  or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
#
#  Alternatively, this plugin may be distributed and executed on hardware which
#  is licensed by Dream Multimedia GmbH.
#
#  This plugin is NOT free software. It is open source, you are allowed to
#  modify it (if you keep the license), but it may not be commercially
#  distributed other than under the conditions noted above.
#
#######################################################################

#######################################################################
#
#   Modified by gigilangh for the Power-Sat Team
#
#######################################################################

from Screens.Screen import Screen
from Screens.ChoiceBox import ChoiceBox
from Screens.Console import Console
from Screens.MessageBox import MessageBox
from Screens.Standby import TryQuitMainloop
from Components.ActionMap import ActionMap
from Components.AVSwitch import AVSwitch
from Components.config import config, configfile, ConfigYesNo, ConfigSubsection, getConfigListEntry, ConfigSelection, ConfigNumber, ConfigText, ConfigInteger
from Components.ConfigList import ConfigListScreen
from Components.Label import Label
from Components.Language import language
from Components.Pixmap import Pixmap
from Components.Sources.StaticText import StaticText
from Plugins.Plugin import PluginDescriptor
from Tools.Directories import fileExists, resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
from enigma import ePicLoad
from skin import parseColor
from os import environ, listdir, remove, rename, system
from twisted.web.client import downloadPage
import gettext
import urllib
import re

#############################################################

config.plugins.MetrixWeather = ConfigSubsection()
config.plugins.MetrixWeather.woeid = ConfigNumber(default = 640161) #Location (visit http://metrixweather.open-store.net/)
config.plugins.MetrixWeather.refreshInterval = ConfigSelection(default = "10", choices = [("10", "10 " + _("minutes")),
																						  ("20", "20 " + _("minutes")),
																						  ("30", "30 " + _("minutes")),
																						  ("60", "60 " + _("minutes"))])

def main(session, **kwargs):
	if session:
		session.open(PowerMetrixFHD)

def changeNothing(reason, **kwargs):
	pass

def Plugins(**kwargs):
	if config.skin.primary_skin.value == "MetrixPS-FHD/skin.xml":
		from Plugins.PowerSat.PowerMetrixFHD.mod import changeModules
		return [PluginDescriptor(name="Power-Sat Metrix FHD", description=_("Setup for Metrix-FHD skin (Power-Sat image)."), where=PluginDescriptor.WHERE_PLUGINMENU, icon="plugin.png", fnc=main),
				PluginDescriptor(where=PluginDescriptor.WHERE_AUTOSTART, fnc=changeModules, needsRestart = False),
				]
	else:
		print "[PowerMetrixFHD] MetrixPS-FHD is not the current skin!!"
		return [PluginDescriptor(where=PluginDescriptor.WHERE_SESSIONSTART, fnc=changeNothing)]

#######################################################################

class PowerMetrixFHD(ConfigListScreen, Screen):
	skin = """<screen name="PowerMetrixFHD" position="40,40" size="1200,640" title="PowerMetrix FHD setup" flags="wfNoBorder" backgroundColor="#40000000">
				<eLabel font="Regular; 20" foregroundColor="#00ffffff" backgroundColor="#40000000" halign="left" position="37,605" size="250,33" text="Cancel" transparent="1" />
				<eLabel font="Regular; 20" foregroundColor="#00ffffff" backgroundColor="#40000000" halign="left" position="335,605" size="250,33" text="OK" transparent="1" />
				<widget name="config" position="21,77" scrollbarMode="showOnDemand" size="590,506" transparent="1" />
				<eLabel position="20,15" size="348,50" text="PowerMetrix FHD setup" font="Regular; 40" valign="center" transparent="1" backgroundColor="#40000000" />
				<eLabel position="320,600" size="5,40" backgroundColor="#0000ff00" />
				<eLabel position="20,600" size="5,40" backgroundColor="#00ff0000" />
			  </screen> """

	def __init__(self, session, args = None, picPath = None):
		self.version = " V1.2"
		Screen.__init__(self, session)
		self.session = session
		self.picPath = picPath
		self.setup_title = _("PowerMetrix FHD setup") + self.version
		list = []
		list.append(getConfigListEntry(_("Metrix weather"))) 
		list.append(getConfigListEntry(_("Metrix weather ID"), config.plugins.MetrixWeather.woeid))
		list.append(getConfigListEntry(_("Refresh interval"), config.plugins.MetrixWeather.refreshInterval))
		ConfigListScreen.__init__(self, list)
		self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
		{
			"cancel": self.keyCancel,
			"red": self.keyCancel,
			"green": self.keySave
		}, -1)
		self["key_red"] = StaticText(_("Cancel"))
		self["key_green"] = StaticText(_("OK"))
		self.onLayoutFinish.append(self.layoutFinished)

	def layoutFinished(self):
		self.setTitle(_(self.setup_title))
