import re
import textwrap
import pprint

import klammer_base
import kutil
import latex_util

class Indent(klammer_base.Klammer_base):
    def __init__(self, K):
        super().__init__(K)
        self.s = kutil.escape(self.s)

    def html(self):
        return "FIX: INDENT " + self.s

    def tex(self):
        tab = f"\\hspace*{{{self.w}ex}}"
        result = ""
        
        if self.linebreak:
            for e in self.s.split("\n"):
                result += f"{tab}{e}\\\\\n"
            result = result[:-3]
            result = re.sub(r"\t", r"\\t", result)
        else:
            result = tab + latex_util.minipage(
                "\\raggedright " + self.s, f"\\textwidth - {self.w}ex", center=False, vmargin="4pt")
        #print(result)
        return result

    def txt(self):
        indent = " " * self.w
        result = self.s
        result = re.sub("KK0022", '"', result)
        result = indent + f"\n{indent}".join(textwrap.wrap(result, width=70, break_on_hyphens=False))
        return f"@lit\n{result}\nlit@"
    
class Note(klammer_base.Klammer_base):
    def __init__(self, K):
        super().__init__(K)

    def html(self):
        color = ",".join([f"{float(e)*100}%" for e in self.color.split(",")])
        result = f'''<div class="box" style="background-color: rgb({color}); border-color: rgb({self.bordercolor}) ;">
<b>{self.label}:</b> {self.s}
</div>'''
        return result

    def tex(self):
        if self.width:
            width = r'{}\\textwidth'.format(self.width)
        else:
            #width = r'\\textwidth - 16pt - {}\\leftmargin'.format(self.level)
            width = r'\\linewidth - \\leftmargin + 2pt'

        result = '''
\\par\\begingroup
COLOR\\setlength{\\fboxsep}{8pt}
  \\fcolorbox{bordercolor}{localcolor}{
   \\parbox{WIDTH}{\\raggedright\\setlength{\\parskip}{8pt}
      \\textbf{LABEL:} TEXT
}}\\endgroup\\par
'''
        result = re.sub('LABEL', self.label, result)
        result = re.sub('TEXT', re.sub(r'\\', r'\\\\', self.s), result)
        result = re.sub('COLOR', r'\\definecolor{{localcolor}}{{rgb}}{{{}}}\nCOLOR'.format(self.color), result)
        result = re.sub('COLOR', r'\\definecolor{{bordercolor}}{{rgb}}{{{}}}\n'.format(self.bordercolor), result)
        result = re.sub('WIDTH', width, result)
        print(result)
        return result


class Block(klammer_base.Klammer_base):
    def __init__(self, K):
        super().__init__(K)
        #self.show()

    def html(self):
        return ""

    def tex(self):
        to_x, to_y = [float(e) for e in self.to.split()]
        pt_x, pt_y  = [float(e) for e in self.point.split()]
        result = f"""
\\begin{{textblock}}{{{self.width}}}[{pt_x},{pt_y}]({to_x},{to_y})
\\vspace*{{-1\\parskip}}
{self.content.strip()}
\\end{{textblock}}
"""
        return result


class Lines(klammer_base.Klammer_base):
    def __init__(self, K):
        super().__init__(K)
        self.s = kutil.escape(self.s)
        self.lines = self.s.split("\n")

    def html(self):
        result = ""
        for line in self.lines:
            result += line + "<br>\n"
        return result

    def tex(self):
        return "\\\\\n".join(self.lines) + "\n"
        result = ""
        for line in self.lines:
            if line:
                line += r" \\"
            result += line + "\n"
        result = re.sub(r"\\\\\n\n", "\n\n", result)
        return result
