import json

import ipyvuetify as v
import pandas as pd
import numpy as np

from component import parameter as cp
from component.message import cm


class PriorityTable(v.SimpleTable):

    _colors = cp.gradient(5)

    _BENEFITS = pd.read_csv(cp.layer_list).fillna("").sort_values(by=["subtheme"])
    _BENEFITS = _BENEFITS[_BENEFITS.theme == "benefit"]

    _DEFAULT_V_MODEL = {layer_id: 4 for layer_id in _BENEFITS.layer_id}

    def __init__(self):

        # construct the checkbox list
        self.checkbox_list = {}
        for layer_id in self._BENEFITS.layer_id:
            line = []
            for i, c in enumerate(self._colors):
                metadata = {"label": layer_id, "val": i}
                check = v.Checkbox(color=c, _metadata=metadata, v_model=i == 4)
                line += [check]
            self.checkbox_list[layer_id] = line

        # construct the rows of the table
        rows, self.btn_list = [], []
        for i, lr in self._BENEFITS.iterrows():
            edit_btn = v.Icon(children=["mdi-pencil"], _metadata={"layer": lr.layer_id})
            self.btn_list.append(edit_btn)

            # start with the layer informations
            row = [
                v.Html(tag="td", children=[edit_btn]),
                v.Html(tag="td", children=[lr.subtheme]),
                v.Html(tag="td", children=[getattr(cm.layers, lr.layer_id).name]),
            ]

            # then add the checkboxes
            for j, c in enumerate(self._colors):
                check = self.checkbox_list[lr.layer_id][j]
                row += [v.Html(tag="td", children=[check])]

            rows.append(v.Html(tag="tr", children=row))

        # generate header using the translator
        headers = v.Html(
            tag="tr",
            children=[
                v.Html(tag="th", children=[cm.benefits.table.action]),
                v.Html(tag="th", children=[cm.benefits.table.theme]),
                v.Html(tag="th", children=[cm.benefits.table.indicator]),
                *[v.Html(tag="th", children=[lbl]) for lbl in cm.benefits.table.labels],
            ],
        )

        # create the table
        super().__init__(
            v_model=json.dumps(self._DEFAULT_V_MODEL),
            children=[
                v.Html(tag="thead", children=[headers]),
                v.Html(tag="tbody", children=rows),
            ],
        )

        # link the checks with the v_model
        for name in self._BENEFITS.layer_id.tolist():
            for check in self.checkbox_list[name]:
                check.observe(self._on_check_change, "v_model")

    def _on_check_change(self, change):

        line = change["owner"]._metadata["label"]

        # if checkbox is unique and change == false recheck
        if change["new"] == False:
            unique = True
            for check in self.checkbox_list[line]:
                if check.v_model == True:
                    unique = False
                    break

            change["owner"].v_model = unique

        else:
            # uncheck all the others in the line
            for check in self.checkbox_list[line]:
                if check != change["owner"]:
                    check.v_model = False

            # change the table model
            tmp = json.loads(self.v_model)
            tmp[line] = change["owner"]._metadata["val"]
            self.v_model = json.dumps(tmp)

        return

    def load_data(self, data):
        """load the data from a questionnaire io"""

        data = json.loads(data)

        # check the appropriate checkboxes
        for k, v in data.items():
            self.checkbox_list[k][v].v_model = True

        return self

import json

import ipyvuetify as v
import pandas as pd
import numpy as np

from component import parameter as cp
from component.message import cm


class PriorityTable(v.SimpleTable):

    _colors = cp.gradient(5)

    _BENEFITS = pd.read_csv(cp.layer_list).fillna("").sort_values(by=["subtheme"])
    _BENEFITS = _BENEFITS[_BENEFITS.theme == "benefit"]

    _DEFAULT_V_MODEL = {layer_id: 4 for layer_id in _BENEFITS.layer_id}

    def __init__(self):

        # construct the checkbox list
        self.checkbox_list = {}
        for layer_id in self._BENEFITS.layer_id:
            line = []
            for i, c in enumerate(self._colors):
                metadata = {"label": layer_id, "val": i}
                check = v.Checkbox(color=c, _metadata=metadata, v_model=i == 4)
                line += [check]
            self.checkbox_list[layer_id] = line

        # construct the rows of the table
        rows, self.btn_list = [], []
        for i, lr in self._BENEFITS.iterrows():
            edit_btn = v.Icon(children=["mdi-pencil"], _metadata={"layer": lr.layer_id})
            self.btn_list.append(edit_btn)

            # start with the layer informations
            row = [
                v.Html(tag="td", children=[edit_btn]),
                v.Html(tag="td", children=[lr.subtheme]),
                v.Html(tag="td", children=[getattr(cm.layers, lr.layer_id).name]),
            ]

            # then add the checkboxes
            for j, c in enumerate(self._colors):
                check = self.checkbox_list[lr.layer_id][j]
                row += [v.Html(tag="td", children=[check])]

            rows.append(v.Html(tag="tr", children=row))

        # generate header using the translator
        headers = v.Html(
            tag="tr",
            children=[
                v.Html(tag="th", children=[cm.benefits.table.action]),
                v.Html(tag="th", children=[cm.benefits.table.theme]),
                v.Html(tag="th", children=[cm.benefits.table.indicator]),
                *[v.Html(tag="th", children=[lbl]) for lbl in cm.benefits.table.labels],
            ],
        )

        # create the table
        super().__init__(
            v_model=json.dumps(self._DEFAULT_V_MODEL),
            children=[
                v.Html(tag="thead", children=[headers]),
                v.Html(tag="tbody", children=rows),
            ],
        )

        # link the checks with the v_model
        for name in self._BENEFITS.layer_id.tolist():
            for check in self.checkbox_list[name]:
                check.observe(self._on_check_change, "v_model")

    def _on_check_change(self, change):

        line = change["owner"]._metadata["label"]

        # if checkbox is unique and change == false recheck
        if change["new"] == False:
            unique = True
            for check in self.checkbox_list[line]:
                if check.v_model == True:
                    unique = False
                    break

            change["owner"].v_model = unique

        else:
            # uncheck all the others in the line
            for check in self.checkbox_list[line]:
                if check != change["owner"]:
                    check.v_model = False

            # change the table model
            tmp = json.loads(self.v_model)
            tmp[line] = change["owner"]._metadata["val"]
            self.v_model = json.dumps(tmp)

        return

    def load_data(self, data):
        """load the data from a questionnaire io"""

        data = json.loads(data)

        # check the appropriate checkboxes
        for k, v in data.items():
            self.checkbox_list[k][v].v_model = True

        return self

# construct the checkbox list # construct the rows of the table # start with the layer informations # then add the checkboxes # generate header using the translator # create the table # link the checks with the v_model # if checkbox is unique and change == false recheck # uncheck all the others in the line # change the table model load the data from a questionnaire io # check the appropriate checkboxes