/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global window, document, CSInterface*/


/*

    Responsible for overwriting CSS at runtime according to CC app
    settings as defined by the end user.

*/

var themeManager = (function () {
    'use strict';
     
    /**
     * Convert the Color object to string in hexadecimal format;
     */
    function toHex(color, delta) {
        
        function computeValue(value, delta) {
            var computedValue = !isNaN(delta) ? value + delta : value;
            if (computedValue < 0) {
                computedValue = 0;
            } else if (computedValue > 255) {
                computedValue = 255;
            }
            
            computedValue = Math.floor(computedValue);
    
            computedValue = computedValue.toString(16);
            return computedValue.length === 1 ? "0" + computedValue : computedValue;
        }
    
        var hex = "";
        if (color) {
            hex = computeValue(color.red, delta) + computeValue(color.green, delta) + computeValue(color.blue, delta);
        }
        return hex;
    }


    function reverseColor(color, delta) {
        return toHex({
            red: Math.abs(255 - color.red),
            green: Math.abs(255 - color.green),
            blue: Math.abs(255 - color.blue)
        },
            delta);
    }
            

    function addRule(stylesheetId, selector, rule) {
        var stylesheet = document.getElementById(stylesheetId);
        
        if (stylesheet) {
            stylesheet = stylesheet.sheet;
            if (stylesheet.addRule) {
                stylesheet.addRule(selector, rule);
            } else if (stylesheet.insertRule) {
                stylesheet.insertRule(selector + ' { ' + rule + ' }', stylesheet.cssRules.length);
            }
        }
    }
        
        
                
    /**
     * Update the theme with the AppSkinInfo retrieved from the host product.
     */
    function updateThemeWithAppSkinInfo(appSkinInfo) {
        
        var panelBgColor = appSkinInfo.panelBackgroundColor.color;
        var bgdColor = toHex(panelBgColor, -30);
       
        var darkBgdColor =  toHex(panelBgColor, 2);
        var superDarkBgdColor =  toHex(panelBgColor, -20);
        
        var fontColor = "848484";
        if (panelBgColor.red > 122) {
            fontColor = "a2a2a2";
        }
        
        var IconfontColor = "4d4d4d";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            IconfontColor = "ff0000"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            IconfontColor = "ffc0cb"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            IconfontColor = "c0fff4"; }
        

        

        
//        -----------------------------------------------------------
        
        
        var BkgrndColor = "141414";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            BkgrndColor = "262626"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            BkgrndColor = "a3a3a3"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            BkgrndColor = "dbdbdb"; }
        
        var BkgrndColorV = "141414";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            BkgrndColorV = "262626"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            BkgrndColorV = "b8b8b8"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            BkgrndColorV = "f0f0f0"; }
         
        var TrackScrollbar = "212121";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            TrackScrollbar = "1f1f1f"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            TrackScrollbar = "808080"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            TrackScrollbar = "b2b2b2"; }
        
        var ThumbScrollbar = "2e2e2e";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            ThumbScrollbar = "3b3b3b"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            ThumbScrollbar = "999999"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            ThumbScrollbar = "d1d1d1"; }
        
        var AccordionColor = "262626";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            AccordionColor = "424242"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            AccordionColor = "a3a3a3"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            AccordionColor = "dbdbdb"; }
        
        var AccordionFontC = "808080";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            AccordionFontC = "999999"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            AccordionFontC = "3f3f3f"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            AccordionFontC = "595959"; }
        
        var PanelTitleText = "343434";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            PanelTitleText = "363636"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            PanelTitleText = "7a7a7a"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            PanelTitleText = "b2b2b2"; }
        
        var SliderColor = "3b3b3b";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            SliderColor = "424242"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            SliderColor = "666666"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            SliderColor = "8c8c8c"; }
        
        var SliderThumb = "3b3b3b";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            SliderThumb = "4a4a4a"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            SliderThumb = "ababab"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            SliderThumb = "e5e5e5"; }
        
        var SliderBorderColor = "141414";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            SliderBorderColor = "1f1f1f"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            SliderBorderColor = "808080"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            SliderBorderColor = "b8b8b8"; }
        
        var SliderThumbBorder = "141414";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            SliderThumbBorder = "1f1f1f"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            SliderThumbBorder = "808080"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            SliderThumbBorder = "b8b8b8"; }
        
        var HeaderForDivColor = "595959";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            HeaderForDivColor = "1f1f1f"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            HeaderForDivColor = "808080"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            HeaderForDivColor = "b8b8b8"; }
        
        var RadioButtonDotColor = "dedede";
        if (panelBgColor.red >= 65 && panelBgColor.red <= 170) {
            RadioButtonDotColor = "dedede"; }
        else if (panelBgColor.red >= 170 && panelBgColor.red <= 190) {
            RadioButtonDotColor = "d9d9d9"; }
        else if (panelBgColor.red >= 200 && panelBgColor.red <= 255) {
            RadioButtonDotColor = "f2f2f2"; }

        var OverlayHoverColor = "404040";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            OverlayHoverColor = "5c5c5c"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            OverlayHoverColor = "bdbdbd"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            OverlayHoverColor = "f5f5f5"; }
        
        var OverlayActiveColor = "333333";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            OverlayActiveColor = "4f4f4f"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            OverlayActiveColor = "b0b0b0"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            OverlayActiveColor = "e8e8e8"; }
        
        var NavBarButtons = "e5e5e5";
        if (panelBgColor.red > 65 && panelBgColor.red < 170) {
            NavBarButtons = "d9d9d9"; }
        else if (panelBgColor.red > 170 && panelBgColor.red < 200) {
            NavBarButtons = "262626"; }
        else if (panelBgColor.red > 200 && panelBgColor.red < 255) {
            NavBarButtons = "262626"; }

//        -----------------------------------------------------------        
        
        var lightBgdColor = toHex(panelBgColor, -100);
        var superlightBgdColor = toHex(panelBgColor, -190);
                
        var styleId = "hostStyle";
        

        //Background colors
        addRule(styleId, ".hostElt", "background-color:" + "#" + BkgrndColorV);
        
   
        //Duotone Panel text h12
        addRule(styleId, ".bottomText", "color:" + "#" + PanelTitleText);
        addRule(styleId, ".description", "color:" + "#" + PanelTitleText);
        
        //Scrollbar Slider        
        addRule(styleId, "::-webkit-scrollbar-track", "background-color:" + "#" + TrackScrollbar);
        addRule(styleId, "::-webkit-scrollbar-thumb", "background-color:" + "#" + ThumbScrollbar);
        addRule(styleId, "::-webkit-scrollbar-thumb:window-inactive", "background-color:" + "#" + ThumbScrollbar);
        
        //Slider
        addRule(styleId, ".range", "background-color:" + "#" + SliderColor);
        addRule(styleId, ".range::-webkit-slider-runnable-track", "background:" + "#" + SliderColor);
        addRule(styleId, ".range:focus", "background:" + "#" + SliderColor);
        
        addRule(styleId, ".range", "border-color:" + "#" + SliderBorderColor);
        
        addRule(styleId, ".range::-webkit-slider-thumb", "background-color:" + "#" + SliderThumb);
        addRule(styleId, ".range::-webkit-slider-thumb", "border-color:" + "#" + SliderThumbBorder);

        addRule(styleId, ".range::-webkit-slider-thumb:hover", "background-color:" + "#" + OverlayHoverColor);
        
        //ALL TEXTS
        addRule(styleId, "p", "color:" + "#" + AccordionFontC);
        addRule(styleId, "h8", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#PatLabel1", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#PatLabel2", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#BgFill1", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#BgDel", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#label-text1", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#label-text2", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#label-text3", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#label-text4", "color:" + "#" + AccordionFontC);
        addRule(styleId, "#checktitle", "color:" + "#" + AccordionFontC);
        
        addRule(styleId, ".divide1", "background-color:" + "#" + HeaderForDivColor);
        addRule(styleId, ".divide2", "background-color:" + "#" + HeaderForDivColor);
        
        
        //Buttons
        addRule(styleId, ".topcoat-button--large", "background-color:" + "#" + AccordionColor);
        addRule(styleId, ".topcoat-button--large:hover", "background-color:" + "#" + OverlayHoverColor);
        addRule(styleId, ".topcoat-button--large:active", "background-color:" + "#" + OverlayActiveColor);
        
        addRule(styleId, ".topcoat-button--large", "color:" + "#" + AccordionFontC);
        
        addRule(styleId, "#btn_01", "background-color:" + "#" + BkgrndColorV);
        addRule(styleId, "#btn_02", "background-color:" + "#" + BkgrndColorV);
        addRule(styleId, "#btn_03", "background-color:" + "#" + BkgrndColorV);
        
        addRule(styleId, "#btn_01", "color:" + "#" + NavBarButtons);
        addRule(styleId, "#btn_02", "color:" + "#" + NavBarButtons);
        addRule(styleId, "#btn_03", "color:" + "#" + NavBarButtons);

        //Checkbox
        addRule(styleId, ".checkmark", "background-color:" + "#" + SliderColor);
        addRule(styleId, ".cont:hover input ~ .checkmark", "background-color:" + "#" + OverlayHoverColor);
        addRule(styleId, ".cont input:checked ~ .checkmark", "background-color:" + "#2196F3");

    
    }
    

    
    
    
    
    function onAppThemeColorChanged(event) {
        var skinInfo = JSON.parse(window.__adobe_cep__.getHostEnvironment()).appSkinInfo;
        updateThemeWithAppSkinInfo(skinInfo);
    }


    function init() {
        
        var csInterface = new CSInterface();
    
        updateThemeWithAppSkinInfo(csInterface.hostEnvironment.appSkinInfo);
        
        csInterface.addEventListener(CSInterface.THEME_COLOR_CHANGED_EVENT, onAppThemeColorChanged);
    }
    
    return {
        init: init
    };
    
}());


    
    
