#!/usr/bin/python3

#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Based on (forked) CDEtheme for XFCE by Jos van Riswick
# Licence: GPLv3
#

import re
import os
import sys
import shutil

try:
   from PyQt4 import QtCore, QtGui
except ImportError:
   from PyQt5 import QtCore, QtGui

from SpritesGtk2 import spriteLWHXYgtk2
import MotifColors
import Globals
from MiscFun import *

# Generate gtkrc-2.0 #######################################
# generates only the color definitions cdecolors.rc

def gengtk2colors(filename,opts):

    lines="""
#
# Generated by MotifColors.py for palette {opts.currentpalettefile}
# Edits will be overwritten
#
    """.format(**locals())

    lines+="""
gtk-color-scheme = "base_color:{bg_color_5}\\nfg_color:{fg_color_5}\\ntooltip_fg_color:{fg_color_5}\\nselected_bg_color:{sel_color_5}\\nselected_fg_color:{fg_color_5}\\ntext_color:{fg_color_5}\\nbg_color:{bg_color_5}\\ninsensitive_bg_color:{sel_color_5}\\ntooltip_bg_color:{sel_color_5}"
    """.format(**Globals.colorshash)

    for a in range(1,9):
        bg=Globals.colorshash['bg_color_'+str(a)]
        ts=Globals.colorshash['ts_color_'+str(a)]
        bs=Globals.colorshash['bs_color_'+str(a)]
        fg=Globals.colorshash['fg_color_'+str(a)]
        sel=Globals.colorshash['sel_color_'+str(a)]
        lines+="""
style "cde_style_{a}"
{{
    fg[NORMAL]  ="{fg}"
    bg[NORMAL]  ="{bg}"
    text[NORMAL]="{fg}"
    base[NORMAL]="{bg}"

    fg[ACTIVE]  ="{fg}"
    bg[ACTIVE]  ="{sel}"
    text[ACTIVE]="{fg}"
    base[ACTIVE]="{sel}"

    fg[PRELIGHT]="{fg}"
    bg[PRELIGHT]="{bg}"
    text[PRELIGHT]="{fg}"
    base[PRELIGHT] ="{bg}"

    fg[SELECTED]="{bg}"
    bg[SELECTED]="{fg}"
    text[SELECTED]="{bg}"
    base[SELECTED]="{fg}"

    # This is the dark color of the etched in insensitive text when on normal background of widget
    fg[INSENSITIVE]="{bs}"
    bg[INSENSITIVE]="{bg}"
    # Insensitive text color in entry and also fg color of etched in text in combo box
    text[INSENSITIVE]="{bs}"
    # In cde this is normal bg color
    base[INSENSITIVE]="{bg}"
}}
""".format(**locals())


    for a in range(1,9):
        bg=Globals.colorshash['bg_color_'+str(a)]
        ts=Globals.colorshash['ts_color_'+str(a)]
        bs=Globals.colorshash['bs_color_'+str(a)]
        fg=Globals.colorshash['fg_color_'+str(a)]
        sel=Globals.colorshash['sel_color_'+str(a)]
        lines+="""
style "cde_style_sel_{a}"
{{
    fg[NORMAL]  ="{fg}"
    bg[NORMAL]  ="{sel}"
    text[NORMAL]="{fg}"
    base[NORMAL]="{sel}"

    fg[ACTIVE]  ="{fg}"
    bg[ACTIVE]  ="{bg}"
    text[ACTIVE]="{fg}"
    base[ACTIVE]="{bg}"

    fg[PRELIGHT]="{fg}"
    bg[PRELIGHT]="{bs}"
    text[PRELIGHT]="{fg}"
    base[PRELIGHT] ="{bs}"

    fg[SELECTED]="{fg}"
    bg[SELECTED]="{bs}"
    text[SELECTED]="{fg}"
    base[SELECTED]="{bs}"

    fg[INSENSITIVE]="{fg}"
    bg[INSENSITIVE]="{sel}"
    text[INSENSITIVE]="{fg}"
    base[INSENSITIVE]="{sel}"
}}
""".format(**locals())

    with open(filename, 'w') as f: 
        for l in lines:
            f.write(l)

# Generate gtkrc 3 color definitions
def gengtk3colors(filename,opts):

    lines="""
/*
 Generated by MotifColors.py for palette {opts.currentpalettefile}
 Edits will be overwritten
*/
""".format(**locals())
    for a in range(1,9):
        bg=Globals.colorshash['bg_color_'+str(a)]
        ts=Globals.colorshash['ts_color_'+str(a)]
        bs=Globals.colorshash['bs_color_'+str(a)]
        fg=Globals.colorshash['fg_color_'+str(a)]
        sel=Globals.colorshash['sel_color_'+str(a)]
        lines+="""
@define-color bg_color_{a} {bg}; 
@define-color fg_color_{a} {fg}; 
@define-color ts_color_{a} {ts}; 
@define-color bs_color_{a} {bs}; 
@define-color sel_color_{a} {sel}; 

""".format(**locals())

    with open(filename, 'w') as f: 
        for l in lines:
            f.write(l)

def replaceColorsInPixmap(pixmap,fromcolor,tocolor):
        mask = pixmap.createMaskFromColor(QtGui.QColor(fromcolor), QtCore.Qt.MaskOutColor)
        p = QtGui.QPainter(pixmap)
        p.setPen(QtGui.QColor(tocolor))
        p.drawPixmap(pixmap.rect(), mask, mask.rect())
        p.end()

def updateThemeImages(opts):
    gtk2resourcedir=os.path.join(Globals.userthemedir,'img2')
    gtk2resourcefile=os.path.join(gtk2resourcedir,'resource.indexed.png')
    print (gtk2resourcefile)
    motifcolors=Globals.colorshash

    colormap =[     
        ['#000100','fg_color_5'], 
        ['#69605b','bs_color_5'], 
        ['#a9988d','sel_color_5'], 
        ['#eda86e','bg_color_1'], 
        ['#c5b3a8','bg_color_5'], 
        ['#e7ded7','ts_color_5']
    ]

    tmppixmap=QtGui.QPixmap(gtk2resourcefile)
    for i in range(len(colormap)):
        fromcolor=colormap[i][0] #['#464D54','bs_color_3']
        tocolor=motifcolors[colormap[i][1]]#motifcolors[ts_color_4]=#72ab84
        replaceColorsInPixmap(tmppixmap,fromcolor,tocolor)

    targetdir=os.path.join(gtk2resourcedir,'colorset5')

    for l in spriteLWHXYgtk2:
        label=l[0]
        rect=QtCore.QRect(l[3],l[4],l[1],l[2]) #qrect=xywh i prefer WxH+x+y
        img=tmppixmap.copy(rect)
        targetimagefile=os.path.join(targetdir,label+'.png')
        img.save(targetimagefile)

    colormap =[     
        ['#000100','fg_color_6'], 
        ['#69605b','bs_color_6'], 
        ['#a9988d','sel_color_6'], 
        ['#eda86e','bg_color_1'], 
        ['#c5b3a8','bg_color_6'], 
        ['#e7ded7','ts_color_6']
    ]

    tmppixmap=QtGui.QPixmap(gtk2resourcefile)
    for i in range(len(colormap)):
        fromcolor=colormap[i][0] #['#464D54','bs_color_3']
        tocolor=motifcolors[colormap[i][1]]#motifcolors[ts_color_4]=#72ab84
        replaceColorsInPixmap(tmppixmap,fromcolor,tocolor)

    targetdir=os.path.join(gtk2resourcedir,'colorset6')
    print (targetdir)

    for l in spriteLWHXYgtk2:
        label=l[0]
        rect=QtCore.QRect(l[3],l[4],l[1],l[2]) #qrect=xywh i prefer WxH+x+y
        img=tmppixmap.copy(rect)
        targetimagefile=os.path.join(targetdir,label+'.png')
        img.save(targetimagefile)

    colormap =[     
        ['#000100','fg_color_4'], 
        ['#69605b','bs_color_4'], 
        ['#a9988d','sel_color_4'], 
        ['#eda86e','bg_color_1'], 
        ['#c5b3a8','bg_color_4'], 
        ['#e7ded7','ts_color_4']
    ]

    tmppixmap=QtGui.QPixmap(gtk2resourcefile)
    for i in range(len(colormap)):
        fromcolor=colormap[i][0] #['#464D54','bs_color_3']
        tocolor=motifcolors[colormap[i][1]]#motifcolors[ts_color_4]=#72ab84
        replaceColorsInPixmap(tmppixmap,fromcolor,tocolor)

    targetdir=os.path.join(gtk2resourcedir,'colorset4')
    print (targetdir)

    for l in spriteLWHXYgtk2:
        label=l[0]
        rect=QtCore.QRect(l[3],l[4],l[1],l[2]) #qrect=xywh i prefer WxH+x+y
        img=tmppixmap.copy(rect)
        targetimagefile=os.path.join(targetdir,label+'.png')
        img.save(targetimagefile)

