"""Subclass of frmMorph, which is generated by wxFormBuilder."""

import wx
import TextmorphGui
from mylib import word_count


# Implementing frmMorph
class frmMorph(TextmorphGui.frmMorph):
    def __init__(self, parent):
        TextmorphGui.frmMorph.__init__(self, parent)
        self.process_list = []

    def tell_word_count(self):
        text_message = "%s (%s) words" % (word_count(self.txt_input.Value), word_count(self.txt_processed.Value))
        self.status_bar.SetStatusText(text_message, i=0)

    def tell_char_count(self):
        text_message = "%s (%s) characters" % (len(self.txt_input.Value), len(self.txt_processed.Value))
        self.status_bar.SetStatusText(text_message, i=1)

    def process_text(self):
        buffer = str(self.txt_input.Value)
        for func in self.process_list:
            if func == str.replace:
                buffer = func(buffer, str(self.txt_search_text.Value), str(self.txt_replace_text.Value))
            else:
                buffer = func(buffer)
        self.txt_processed.Value = buffer
        self.tell_word_count()
        self.tell_char_count()

    def on_search_text(self, event):
        self.process_text()

    def on_replace_text(self, event):
        self.process_text()

    def on_input_text_changed(self, event):
        self.process_text()

    def on_toggle_search_replace(self, event):
        if self.chk_search_replace.Value:
            self.txt_search_text.Enable()
            self.txt_replace_text.Enable()
            self.process_list.append(str.replace)
        else:
            self.txt_search_text.Disable()
            self.txt_replace_text.Disable()
            if str.replace in self.process_list:
                self.process_list.remove(str.replace)

    def clear_lowercase(self):
        if self.chk_modify_text_lower.Value:
            self.chk_modify_text_lower.Value = False
        if str.lower in self.process_list:
            self.process_list.remove(str.lower)

    def clear_uppercase(self):
        if self.chk_modify_text_upper.Value:
            self.chk_modify_text_upper.Value = False
        if str.upper in self.process_list:
            self.process_list.remove(str.upper)

    def clear_titlecase(self):
        if self.chk_modify_text_proper.Value:
            self.chk_modify_text_proper.Value = False
        if str.title in self.process_list:
            self.process_list.remove(str.title)

    def on_toggle_lowercase(self, event):
        self.clear_titlecase()
        self.clear_uppercase()
        if self.chk_modify_text_lower.Value:
            self.process_list.append(str.lower)
        else:
            self.clear_lowercase()
        self.process_text()

    def on_toggle_titlecase(self, event):
        self.clear_lowercase()
        self.clear_uppercase()
        if self.chk_modify_text_proper.Value:
            self.process_list.append(str.title)
        else:
            self.clear_titlecase()
        self.process_text()

    def on_toggle_uppercase(self, event):
        self.clear_lowercase()
        self.clear_titlecase()
        if self.chk_modify_text_upper.Value:
            self.process_list.append(str.upper)
        else:
            self.clear_uppercase()
        self.process_text()

    def on_close(self, event):
        self.Destroy()


app = wx.App(redirect=False)
# app = wx.App()
frame = frmMorph(None)
frame.Show(True)
app.MainLoop()