import border
import font
import html_util
from html_util import E

class Cell:
    def __init__(self, text, font, hpos,
                 top, right, bottom, left,
                 left_all, right_all,
                 rowspan, colspan):
        self.text = text
        self.font = font
        self.hpos = hpos
        self.border = border.Border(top, right, bottom, left, left_all, right_all)
        self.rowspan = rowspan
        self.colspan = colspan
        #print("Cell:", text, hpos)

    def __str__(self):
        b = str(self.border)
        b = f"[{b}]" if b else ""
        rs = f"[r{self.rowspan}]" if self.rowspan else ""
        cs = f"[r{self.colspan}]" if self.colspan else ""
        #return f'<cell"{self.text}"{self.font}{self.hpos}{b}>'
        return f'<cell"{self.text}"{b}{rs}{cs}>'

    def __repr__(self):
        return self.__str__()

    def html(self):
        result = self.text
        result = E("td").body(font.html_fontify(result, self.font, 1.0))
        for pred, cls_name in zip(self.border.has(), "Bt Br Bb Bl".split()):
            if pred:
                result.cls(cls_name)
        result.cls(f"H{self.hpos}")
        return result.str()

    def tex(self, debug=False): # , left_line, right_line):
        result = self.text
        if self.font != "r":
            result = font.tex_fontify(result, self.font, 1.0)
        remove_left = self.border.left_all and not self.border.left
        remove_right = self.border.right_all and not self.border.right
        if debug:
            left_marker = "xL" if remove_left else "L" if self.border.left_all else ""
            right_marker = "Rx" if remove_right else "R" if self.border.right_all else ""
            result = f"{left_marker} {result} {right_marker}"
        if remove_left or remove_right:
            pos = self.hpos
            if self.border.left_all and self.border.left:
                pos = "|" + pos
            if self.border.right_all and self.border.right:
                pos = pos + "|"
            result = f"\\multicolumn{{1}}{{{pos}}}{{{result}}}"
        return result


if __name__ == "__main__":
    print(Cell("dog", "r", "c", True, False, True, False))
