import cocos
from cocos.director import director
from app.verkolst import verkolst


class ProvinceInfo(cocos.layer.Layer):

    is_event_handler = False

    def __init__(self):
        super().__init__()
        self.position = (director.window.width+450, director.window.height)

        self.province = None

        background = cocos.sprite.Sprite('province_info_bg.png', position=(0, 0), anchor=(450, 300))
        self.add(background)

        self.terrain_type_label = cocos.text.Label("-", font_size=10, position=(-450+30, -30))
        self.add(self.terrain_type_label)

        self.lc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -45))
        self.add(self.lc_pops_label)

        self.mc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -60))
        self.add(self.mc_pops_label)

        self.uc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -75))
        self.add(self.uc_pops_label)

        self.food_production_label = cocos.text.Label("-", font_size=10, position=(-450+30, -90))
        self.add(self.food_production_label)

        self.food_consumption_label = cocos.text.Label("-", font_size=10, position=(-450+30, -105))
        self.add(self.food_consumption_label)

        self.food_rate_label = cocos.text.Label("-", font_size=10, position=(-450+30, -120))
        self.add(self.food_rate_label)

        self.food_stored_label = cocos.text.Label("-", font_size=10, position=(-450+30, -135))
        self.add(self.food_stored_label)

        self.trade_value_label = cocos.text.Label("-", font_size=10, position=(-450+30, -150))
        self.add(self.trade_value_label)

        self.export_partner_label = cocos.text.Label("-", font_size=10, position=(-450+30, -165))
        self.add(self.export_partner_label)

    def set_province(self, province):
        if self.province is None and province is not None:
            self.do(cocos.actions.MoveTo((director.window.width, director.window.height), 0.1))
        elif self.province is not None and province is None:
            self.do(cocos.actions.MoveTo((director.window.width+450, director.window.height), 0.1))

        self.province = province
        self._render()

    def _render(self):
        if self.province is not None:
            self.terrain_type_label.element.text = (f'{self.province.tile.properties["name"]}'
                                                    f' ({self.province.i}, {self.province.j})')
            i, j = self.province.i, self.province.j
            resources = verkolst.resources

            pops_tot = resources.lc_pops[i][j] + resources.mc_pops[i][j] + resources.uc_pops[i][j]

            def _fmt(label, number, starving):
                if pops_tot > 0:
                    if starving[i][j]:
                        hl_color = "#B22222"  # Red
                    elif resources.any_starvation[i][j]:
                        hl_color = "#DAA520"  # Yellow
                    else:
                        hl_color = "#FFFFF0"  # Off White
                    return (f'<font color="#FFFFF0">{label}: </font><font color="{hl_color}">{int(number[i][j]):d}</font>'
                            f'<font color="#FFFFF0"> - {100*number[i][j]/pops_tot:.1f}%</font>'
                            )
                else:
                    return f'<font color="#FFFFF0">{label}: - </font>'

            self.lc_pops_label.element.text = _fmt('LC', resources.lc_pops, resources.lc_starvation)
            self.mc_pops_label.element.text = _fmt('MC', resources.mc_pops, resources.mc_starvation)
            self.uc_pops_label.element.text = _fmt('UC', resources.uc_pops, resources.uc_starvation)

            self.food_production_label.element.text = f"Food production: {resources.food_production[i][j]:.1f}"
            self.food_consumption_label.element.text = f"Food consumption: {resources.food_consumption[i][j]:.1f}"
            self.food_rate_label.element.text = f"Food production rate: {resources.food_rate[i][j]:.1f}"
            self.food_stored_label.element.text = f"Food stored: {resources.food_stored[i][j]:.1f}"

            self.trade_value_label.element.text = f"Trade value: {resources.trade_value[i][j]:.1f}"

            self.export_partner_label.element.text = f"Exporting to: ({resources.export_partner[i][j][0]:.1f}, {resources.export_partner[i][j][1]:.1f})"

    def on_turn_start(self):
        self._render()

