import tkinter as tk
from tkinter import ttk, Text, scrolledtext
#from tkinter import Tk, Label, Button

class App(tk.Tk):
  def __init__(self):
    super().__init__()

    # configure the root window
    self.title('VetTextOop')
    self.geometry('600x450')
    
    self.text0 = Text(self, height=3, width=60) # for instructions
    self.text0.pack()
    self.text0.insert('1.0', 'Paste text into first text area')
    self.text0.insert('2.0', '\nThen press the copy button')
    self.text0.insert('3.0', '\nCopied text will be clear of non ascii characters!')

    # accepts pasted code to be converted
    self.text1 = scrolledtext.ScrolledText(self, height=8, width=60) # text input
    self.text1.pack()

    # button
    self.button = ttk.Button(self, text='Copy')
    self.button['command'] = self.button_clicked
    self.button.pack()

    #contains converted code after button clicked
    self.text2 = scrolledtext.ScrolledText(self, height=8, width=60) # text output
    self.text2.pack()
  
    #contains status and error messages
    self.text3 = scrolledtext.ScrolledText(self, height=8, width=60) # error output
    self.text3.pack()
  
  def button_clicked(self):
    self.doAction()
 
  def doAction(self):
    text_content = self.text1.get('1.0','end')
    ln = 1 # line number (starts at 1)
    cn = 0 # column number (starts at 0)
    self.convertedQty = 0
    self.checked = 0

    for c in text_content:
      if c == '\n': # track line and column numbers
        ln += 1
        cn = 0
      c = self.validateCharacter(c, ln, cn)
      if ord(c) != 0:
        self.text2.insert('end', c)
        self.checked += 1;
      cn += 1
    self.text3.insert('end', "Converted " + str(self.checked) + " characters\n")
      
  # validate char
  # return same or replaced char or 0 if not replacable 
  def validateCharacter(self, c, ln, cn):
    i = ord(c)
    if i > 127:
      if i == 8220 or i == 8221:
        c = '"'
        self.convertedQty += 1
      elif i == 160:
        c = ' '
        self.convertedQty += 1
      else:
        # report unknown input char out of range 
        print (str(ln) + "." + str(cn) + ": " + c + " -> " + str(i))
        c = chr(0)
 
    return c

 
    
app = App()
app.mainloop()