# -*- coding: utf-8 -*-

import pygame
import config
import ConfigParser

from util_funcs  import FileExists

class CaseConfigParser(ConfigParser.SafeConfigParser):
    def optionxform(self, optionstr):
        return optionstr

class SkinManager(object):
    """
    _HighColor = pygame.Color(51,166,255) # #33a6ff
    _TextColor = pygame.Color(83,83,83) # #535353
    _FrontColor = pygame.Color(131,199,219) ## light blue,#83c7db
    _URLColor  = pygame.Color(51,166,255) ## blue more #33a6ff
    _LineColor = pygame.Color(169,169,169)  # #a9a9a9
    _TitleBgColor = pygame.Color(228,228,228)  # #e4e4e4
    _ActiveColor = pygame.Color(175,90,0) ## light brown  #af5a00
    """
    
    _Colors = {}
    _Config = None
    _Fonts = {}
    
    #backup skin
    DefaultSkin = "../skin/default"

    def __init__(self):
        self.Init()

    def ConvertToRGB(self,hexstr):
        
        h = hexstr.lstrip('#')
        return tuple(int(h[i:i+2], 16) for i in (0, 2 ,4))
    
    def Init(self):
        if not SkinManager._Colors:
            self.SetColors()
        if not SkinManager._Fonts:
            self.SetFonts()
    
    def SetFonts(self):
        if not pygame.font.get_init():
            pygame.font.init()
            
        skinpath = config.SKIN+"/truetype"
        fonts_path = {}
        fonts_path["varela"]   = "%s/VarelaRound-Regular.ttf" % skinpath
        fonts_path["veramono"] = "%s/VeraMono.ttf" % skinpath
        fonts_path["noto"]     = "%s/NotoSansMono-Regular.ttf" % skinpath
        fonts_path["notocjk"]     = "%s/NotoSansCJK-Regular.ttf" % skinpath
     	fonts_path["Eurostile"] = "%s/EurostileMN-Medium.pfb.ttf" %skinpath
        #print(fonts_path["Eurostile"])
        fonts_path["EurostileBold"] = "%s/EurostileMN-ExtendedBold.pfb.ttf" % skinpath
        
        for i in range(10,29):
          self._Fonts["varela%d"%i] = pygame.font.Font(fonts_path["varela"],i)
        
	for i in range(10,26):
            self._Fonts["veramono%d"%i] = pygame.font.Font(fonts_path["veramono"],i)
        
        for i in range(10,28):
            self._Fonts["notosansmono%d"%i] = pygame.font.Font(fonts_path["noto"],i)

        for i in range(10,28):
            self._Fonts["notosanscjk%d"%i] = pygame.font.Font(fonts_path["notocjk"],i)

	for i in range(10,29):
            self._Fonts["Eurostile%d"%i] = pygame.font.Font(fonts_path["Eurostile"],i)
	
	self._Fonts["Eurostile11"] = pygame.font.Font(fonts_path["Eurostile"],11)		
        self._Fonts["Eurostile12"] = pygame.font.Font(fonts_path["Eurostile"],12)
        self._Fonts["Eurostile15"] = pygame.font.Font(fonts_path["Eurostile"],15)
	self._Fonts["Eurostile34"] = pygame.font.Font(fonts_path["Eurostile"],34)
        self._Fonts["Eurostile40"] = pygame.font.Font(fonts_path["Eurostile"],40)
        self._Fonts["Eurostile120"] = pygame.font.Font(fonts_path["Eurostile"],120)

	for i in range(10,30):
	    self._Fonts["EurostileBold%d"%i] = pygame.font.Font(fonts_path["EurostileBold"],i)

	self._Fonts["EurostileBold13"]  = pygame.font.Font(fonts_path["EurostileBold"],13)        
        self._Fonts["EurostileBold30"]  = pygame.font.Font(fonts_path["EurostileBold"],30)

    
        self._Fonts["arial"] = pygame.font.SysFont("arial",16)
        
    def SetColors(self):
        Colors = {}
        Colors["High"] = pygame.Color(51, 166, 255)
        Colors["Text"] = pygame.Color(83, 83, 83)
        Colors["ReadOnlyText"] = pygame.Color(130,130,130)
        Colors["Front"] = pygame.Color(131, 199, 219)
        Colors["URL"] = pygame.Color(51, 166, 255)
        Colors["Line"] = pygame.Color(169, 169, 169)
        Colors["TitleBg"] = pygame.Color(228, 228, 228)
        Colors["Active"] = pygame.Color(175, 90, 0)
        Colors["Inactive"] = pygame.Color(120, 160, 140)
	Colors["Disabled"] = pygame.Color(204, 204, 204)        
	Colors["White"] = pygame.Color(255, 255, 255)
        Colors["Black"] = pygame.Color(0, 0, 0)

        SkinManager._Colors = Colors

        self._Config = CaseConfigParser()

        fname = config.SKIN+"/config.ini"

        try:
            self._Config.read(fname)
        except Exception, e:
            print("read skin config.cfg error %s" % str(e))
            return
        else:
            if "Colors" in self._Config.sections():
                colour_opts = self._Config.options("Colors")
#                print(colour_opts)
                for i in SkinManager._Colors:
                    if i in colour_opts:
                        try:
                            SkinManager._Colors[i] = self.ConvertToRGB(
                                self._Config.get("Colors", i))
                        except Exception, e:
                            print("error in ConvertToRGB %s" % str(e))
                            continue
    
    def GiveFont(self,name):
        return SkinManager._Fonts[name]
        
    def GiveColor(self,name):
        if name in SkinManager._Colors:
            return SkinManager._Colors[name]
        else:
            return  pygame.Color(255,0,0)
    
    def GiveIcon(self,orig_file_or_dir): ## return is string,not Surface
        #doing a wrapper for items under /home/cpi/apps/Menu/*, to be like Menu/GameShell/*
        if orig_file_or_dir.startswith("/home/cpi/apps/Menu"):
            orig_file_or_dir = orig_file_or_dir.replace("/home/cpi/apps/Menu/","../Menu/GameShell/")
    
        if orig_file_or_dir.startswith(".."):
            ret  = orig_file_or_dir.replace("..",config.SKIN)
            if FileExists(ret) == False:
                ret = orig_file_or_dir.replace("..",self.DefaultSkin)
        else:
            ret = config.SKIN+"/sys.py/"+orig_file_or_dir
            if FileExists(ret) == False:
                ret = self.DefaultSkin+"/sys.py/"+orig_file_or_dir
    
        if FileExists( ret ):
            return ret
        else:  ## if not existed both in default or custom skin ,return where it is
            return orig_file_or_dir
            
    def GiveWallpaper(self,png_name):
        #first SKIN/wallpapers/xxxx.png
        #second ../skin/DEOT/wallpapers/xxxx.png
        #finnal gameshell/wallpaper/xxxx.png
        #loading.png,seeyou.png,updating.png,gameover.png,desktopbg.png
        wlp = "/wallpaper/"
        if FileExists(config.SKIN+wlp+png_name):
            return config.SKIN+wlp+png_name
        elif FileExists(self.DefaultSkin+wlp+png_name):
            return self.DefaultSkin+wlp+png_name
        else:
            return "gameshell/wallpaper/"+png_name
            
        
            
##global MySkinManager Handler
MySkinManager = None

def InitMySkinManager():
    global MySkinManager
    if MySkinManager == None:
        MySkinManager = SkinManager()
    

InitMySkinManager()
