# Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2016,2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from odoo import api, fields, models

from odoo.addons.base.models.assetsbundle import AssetsBundle, LessStylesheetAsset


class Theme(models.Model):
    _name = "theme_kit.theme"
    name = fields.Char("Name", required=True)
    top_panel_id = fields.Many2one(
        "theme_kit.top_panel", string="Color Schemes for Top Panel"
    )
    left_panel_id = fields.Many2one(
        "theme_kit.left_panel", string="Color Schemes for Left Panel"
    )
    content_id = fields.Many2one(
        "theme_kit.content", string="Color Schemes for Content"
    )
    custom_css = fields.Text(string="Custom CSS/LESS", default=False)
    custom_js = fields.Text(string="Custom JS", default=False)

    code = fields.Text("Code", help="technical computed field", compute="_compute_code")

    @api.multi
    def _compute_code(self):
        for r in self:
            code = ""
            if r.top_panel_id:
                code = code + r.top_panel_id.less
            if r.left_panel_id:
                code = code + r.left_panel_id.less
            if r.content_id:
                code = code + r.content_id.less
            if r.custom_css:
                code = code + r.custom_css
            if code:
                code = self.generate_less2css(code)
            if r.custom_js:
                js_code = r.custom_js
                js_code = (
  
     "try {"
                    + js_code
                    + """
                    } catch (err) {
                      console.log('Error' + err.name + ":" + err.message + ". " + err.stack);
                      alert('Error' + err.name + ":" + err.message + ". " + err.stack);
                    }"""
                )
                code = (
                    code
                    + """<script type="text/javascript" id="custom_js">"""
                    + js_code
                    + """</script>"""
                )
            r.code = code

    def generate_less2css(self, code):
        bundle = AssetsBundle("theme_kit.dummy", [], [])
        assets = LessStylesheetAsset(bundle, inline=code, url="")
        cmd = assets.get_command()
        source = assets.get_source()
        compiled = bundle.compile_css(cmd, source)
        compiled = (
            """<style type="text/css" id="custom_css">""" + compiled + """</style>"""
        )
        return compiled


class ThemeTopPanel(models.Model):
    _name = "theme_kit.top_panel"

    name = fields.Char("Name", required=True)

    top_panel_bg = fields.Char("Background color", help="Menu Bar color for Top Panel")
    top_panel_bg_active = fields.Boolean(
        default=False, help="Menu Bar color for Top Panel"
    )

    top_panel_border = fields.Char("Border color", help="Border color for Top Panel")
    top_panel_border_active = fields.Boolean(
        default=False, help="Border color for Top Panel"
    )

    top_panel_font = fields.Char("Font color", help="Font color for Top Panel")
    top_panel_font_active = fields.Boolean(
        default=False, help="Font color for Top Panel"
    )

    top_panel_active_item_font = fields.Char(
        "Active item Font color", help="Active item Font color for Top Panel"
    )
    top_panel_active_item_font_active = fields.Boolean(
        default=False, help="Active item Font color for Top Panel"
    )

    top_panel_active_item_bg = fields.Char(
        "Active item Background color",
        help="Active item Background color for Top Panel",
    )
    top_panel_active_item_bg_active = fields.Boolean(
        default=False, help="Active item Background color for Top Panel"
    )

    top_panel_hover_item_font = fields.Char(
        "Hover item Font color", help="Hover item Font color for Top Panel"
    )
    top_panel_hover_item_font_active = fields.Boolean(
        default=False, help="Hover item Font color for Top Panel"
    )

    top_panel_hover_item_bg = fields.Char(
        "Hover item Background color", help="Hover item Background color for Top Panel"
    )
    top_panel_hover_item_bg_active = fields.Boolean(
        default=False, help="Hover item Background color for Top Panel"
    )

    less = fields.Text("less", help="technical computed field", compute="_compute_less")

    @api.multi
    def _compute_less(self):
        for r in self:
            code = ""
            # double {{ will be formated as single {
            if self.top_panel_bg_active:
                code = (
                    code
                    + """
                #oe_main_menu_navbar {{
                    background-color: {theme.top_panel_bg};
                }}
                #oe_main_menu_navbar .dropdown-menu{{
                    background-color: {theme.top_panel_bg};
                }}
                .o_main_navbar {{
                    background-color: {theme.top_panel_bg};
                }}
                .dropdown-menu{{
                    background-color: {theme.top_panel_bg};
                }}
                .o_calendar_container .o_calendar_view .o_calendar_widget .fc-week-number, .o_calendar_container .o_calendar_view .o_calendar_widget .fc-widget-header {{
                    background-color: {theme.top_panel_bg};
                }}
                .o_calendar_container .o_calendar_sidebar_container .ui-datepicker .ui-widget-header {{
                    background-color: {theme.top_panel_bg};
                }}
                .datepicker .table-condensed > thead {{
                    background-color: {theme.top_panel_bg};
                }}

                .datepicker .table-condensed > thead th:hover {{
                    background-color: darken({theme.top_panel_bg}, 15%) !important;
                }}
                """
                )

            if self.top_panel_border_active:
                code = (
                    code
                    + """.o_main_navbar{{
                    border-color: {theme.top_panel_border};
                }}
                #oe_main_menu_navbar{{
                    border-color: {theme.top_panel_border};
                }}
                .o_control_panel {{
                    border-bottom-color: {theme.top_panel_border}!important;
                }}
                .o_form_statusbar .o_arrow_button{{
                    border-color: lighten({theme.top_panel_border}, 40%)!important;
                }}
                .o_form_statusbar .o_arrow_button:before{{
                    border-left-color: lighten({theme.top_panel_border}, 40%)!important;
                }}
                .o_list_view thead {{
                    color: {theme.top_panel_border};
                }}
                .o_list_view thead > tr > th {{
                    border-color: {theme.top_panel_border};
                }}
                """
                )
            if self.top_panel_font_active:
                code = (
                    code
                    + """.o_main_navbar > ul > li > a {{
                    color: {theme.top_panel_font}!important;
                }}
                .navbar-nav li a {{
                    color: {theme.top_panel_font}!important;
                }}
                .o_main_navbar > .o_menu_toggle{{
                    color: {theme.top_panel_font}!important;
                }}
                .o_main_navbar .o_menu_brand {{
                    color: {theme.top_panel_font}!important;
                }}
                .open .dropdown-menu > li a span {{
                    color: {theme.top_panel_font}!important;
                }}
                .open .dropdown-menu > li.dropdown-header {{
                    color: {theme.top_panel_font}!important;
                    font-weight: bolder;
                }}
                .dropdown-menu > li > a {{
                    color: {theme.top_panel_font}!important;
                }}
                .o_calendar_container .o_calendar_view .o_calendar_widget .fc-week-number, .o_calendar_container .o_calendar_vi