import tkinter as tk
from tkinter import scrolledtext,ttk,messagebox
import OCR
import pyperclip
import screen_shoot
from Simplified_Traditional.langconv import Converter
import pickle
import requests
import googletrans

class main():
    def __init__(self,photo=False):
        self.CHINESE_SYMBOL = True
        self.DELETE_SPACE = True
        self.DELETE_WRAP = True
        self.DELETE_TABS = True
        self.CLIPBOARD = True
        self.HALF_WIDTH = 1
        self.SIMPLE = 1
        self.photo = photo
        # self.base_url = "http://106.14.182.45:8899"
        self.base_url = "http://localhost:8899"
        self.trans = googletrans.Translator(service_urls=['translate.google.cn'])
    def show(self):
        root = tk.Tk()
        # root.iconbitmap('./icon.ico')
        root.resizable(width=False, height=False)
        root.title('PDF复制助手 v 1.3--powered by Kevin 邱')
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        width = 660
        height = 730
        root.geometry('%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 5))
        root.deiconify()


        # 窗口置顶
        # root.wm_attributes('-topmost',1)

        # 广播通知
        with open('config', 'rb') as f:
            config = pickle.load(f)
            if config['show_broadcast_message'] == 1 and requests.get(self.base_url+'/new_message').text == "True":
                tk.messagebox.showinfo("广播", requests.get(self.base_url + '/broadcast_message').text)
                config['show_broadcast_message'] = 0
                with open('config', 'wb') as f:
                    pickle.dump(config, f)
        print('broadcast finished')

        # 菜单栏
        menuBar = tk.Menu(root)
        root.config(menu=menuBar)

        def Config():
            top = tk.Toplevel()
            top.resizable(width=False, height=False)
            top.geometry('%dx%d+%d+%d' % (500, 100, (screenwidth - 500) / 2, (screenheight - 100) / 2))
            top.title('关于')

            msg = "开发中。。。"
            Mes = tk.Label(top, text=msg, font=12)
            Mes.pack()
        def About():
            top = tk.Toplevel()
            top.resizable(width=False, height=False)
            top.geometry('%dx%d+%d+%d' % (500, 100, (screenwidth - 500) / 2, (screenheight - 100) / 2))
            top.title('关于')

            msg = "Kevin 邱 制作\n欢迎反馈和交流：qiubinyang98@163.com"
            Mes = tk.Label(top,text=msg,font=12)
            Mes.pack()
        def Response():
            top = tk.Toplevel()
            top.resizable(width=False, height=False)
            top.geometry('%dx%d+%d+%d' % (800, 100, (screenwidth - 800) / 2, (screenheight - 100) / 2))
            top.title('问题解决')

            msg = "有问题请联系：qiubinyang98@163.com\n" + requests.get(self.base_url+"/broadcast_message").text
            Mes = tk.Label(top, text=msg, font=12)
            Mes.pack()
        def Exit():
            root.destroy()

        configMenu = tk.Menu(menuBar, tearoff=0)
        menuBar.add_cascade(label="开始", menu=configMenu)
        configMenu.add_command(label="设置", command=Config)
        configMenu.add_command(label="退出", command=Exit)

        helpMenu = tk.Menu(menuBar, tearoff=0)
        menuBar.add_cascade(label="帮助", menu=helpMenu)
        helpMenu.add_command(label="关于", command=About)
        helpMenu.add_command(label="问题解决", command=Response)

        title = tk.Label(root,
                         text='PDF复制助手',
                         font=('宋体', 15, 'bold'),
                         width=15, height=2
                         )
        title.pack()

        frame = tk.Frame(root)
        frame.pack()
        def checkCall(i):
            if i == 0:
                self.CHINESE_SYMBOL = var[i].get()
            elif i == 1:
                self.DELETE_SPACE = var[i].get()
            elif i == 2:
                self.DELETE_WRAP = var[i].get()
            elif i == 3:
                self.DELETE_TABS = var[i].get()
            elif i == 4:
                self.CLIPBOARD = var[i].get()

        checkstr = ["使用中文符号","去除空格","去除换行","去除制表符","自动复制到剪贴板"]
        checkboxs = []
        var = [tk.BooleanVar() for _ in range(len(checkstr))]
        def init(i):
            checkboxs.append(ttk.Checkbutton(frame, text=checkstr[i], variable=var[i],
                                            onvalue=True, offvalue=False,
                                            command=lambda: checkCall(i)))
            checkboxs[i].pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
            var[i].set(1)
        for i in range(len(checkstr)):
            init(i)

        sub_frame1 = tk.Frame(frame)
        sub_frame1.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        sub_frame2 = tk.Frame(frame)
        sub_frame2.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)

        def radio1():
            self.HALF_WIDTH = radio_var1.get()
        def radio2():
            self.SIMPLE = radio_var2.get()
        radio_var1 = tk.IntVar()
        ttk.Radiobutton(sub_frame1,text='半角',value=1,variable=radio_var1,command=radio1).pack()
        ttk.Radiobutton(sub_frame1,text='全角',value=0,variable=radio_var1,command=radio1).pack()
        radio_var1.set(1)

        radio_var2 = tk.IntVar()
        ttk.Radiobutton(sub_frame2, text='简体', value=1, variable=radio_var2,command=radio2).pack()
        ttk.Radiobutton(sub_frame2, text='繁体', value=0, variable=radio_var2,command=radio2).pack()
        radio_var2.set(1)

        def paste():
            input1.delete('0.0', 'end')
            text = pyperclip.paste()
            input1.insert('insert',text)

        label_frame1 = tk.Frame(root)
        label_frame1.pack(anchor=tk.W)
        label1 = tk.Label(label_frame1,
                          text='贴入复制文字：',
                          font=('微软雅黑', 11, 'bold'),
                          width=15, height=2
                          )
        label1.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        paste_button = ttk.Button(label_frame1, text='从剪贴板中粘贴', command=paste)
        paste_button.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        label0 = tk.Label(label_frame1,
                          text='英文较多建议使用截图识别功能',
                          fg='gray',
                          font=('等线', 9),
                          width=25, height=2
                          )
        label0.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        input1 = scrolledtext.ScrolledText(root, bd=1, height=9, width=60, font=10)
        input1.pack()

        def copy():
            pyperclip.copy(input2.get(0.0, tk.END))
        label_frame2 = tk.Frame(root)
        label_frame2.pack(anchor=tk.W)
        label2 = tk.Label(label_frame2,
                          text='整理文字：',
                          font=('微软雅黑', 11, 'bold'),
                          width=15, height=2
                          )
        label2.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        copy_button = ttk.Button(label_frame2,text='复制到剪贴板',command=copy)
        copy_button.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)
        input2 = scrolledtext.ScrolledText(root, bd=1, height=9, width=60, font=10)
        input2.pack()

        # 放置截图
        if (self.photo):
            photo = tk.PhotoImage(file='temp.png')
            input1.image_create(tk.INSERT, image=photo)
            ocr = OCR.OCR('temp.png')
            input2.insert(tk.INSERT, ocr)

        def organize():
            if input1.get(0.0, tk.END) == "\n":
                messagebox.showerror("错误","输入框不能为空")
                return
            input2.delete('0.0', 'end')
            text = input1.get(0.0, tk.END)
            text = self.words_process(text)
            input2.insert(tk.INSERT, text)

        def trans():
            trans_before = input2.get(0.0,tk.END)
            trans_result = self.trans.translate(trans_before,dest='zh-CN').text
            top = tk.Toplevel()
            top.resizable(width=False, height=False)
            top.geometry('%dx%d+%d+%d' % (700, 500, (screenwidth - 600) / 2, (screenheight - 600) / 2))
            top.title('翻译结果')
            trans_scroll = tk.Scrollbar(top)
            trans_scroll.pack(side=tk.RIGHT, fill=tk.Y)

            Mes = tk.Text(top, font=12)
            Mes.insert(tk.INSERT,trans_result)
            Mes.pack(side=tk.LEFT,fill=tk.Y)

            trans_scroll.config(command=Mes.yview)
            Mes.config(yscrollcommand=trans_scroll.set)

        def shoot():
            root.destroy()
            screen_shoot.begin()
        def shoot_key(event):
            root.destroy()
            screen_shoot.quick_begin()

        submit = ttk.Button(label_frame2,
                           text='整理',
                           # width=15,
                           # font=12,
                           command=organize)
        submit.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)

        trans = ttk.Button(label_frame2,
                            text='翻译',
                            # width=15,
                            # font=12,
                            command=trans)
        trans.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)

        line = tk.Canvas(root, height=30, width=900)
        line.pack()
        line.create_line(0, 20, 900, 20)
        label_frame3 = tk.Frame(root)
        label_frame3.pack(anchor=tk.W)
        label3 = tk.Label(label_frame3,
                          text='使用截图进行文字识别：(快捷键 Ctrl + Alt + A)',
                          font=('宋体', 12, 'bold'),
                          width=50, height=1
                          )
        label3.pack(side=tk.LEFT, anchor=tk.W, expand=tk.YES)


        screen_cut = ttk.Button(root,
                               text='截图识字',
                               width=15,
                               # font=12,
                               command=shoot)
        screen_cut.bind_all('<Control-Alt-a>', shoot_key)
        screen_cut.pack()

        root.mainloop()

    def is_alphabet(self,uchar):
        """判断一个unicode是否是英文字母"""
        if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
            return True
        else:
            return False

    def words_process(self,text):
        if self.DELETE_SPACE:
            text = text.replace(' ', '')
        if self.DELETE_WRAP:
            while (text.find('\n') != -1):
                i = text.find('\n')
                if i-1>0 and i+1 < len(text) and self.is_alphabet(text[i - 1]) and self.is_alphabet(text[i + 1]):
                    text = text[:i] + " " + text[i + 1:]
                else:
                    text = text[:i] + text[i + 1:]
        if self.DELETE_TABS:
            text = text.replace('\t', '')
        if self.CLIPBOARD:
            pyperclip.copy(text)
        if self.HALF_WIDTH == 1:
            rstring = ""
            for uchar in text:
                inside_code = ord(uchar)
                if inside_code == 12288:  # 全角空格直接转换
                    inside_code = 32
                elif (inside_code >= 65281 and inside_code <= 65374):  # 全角字符（除空格）根据关系转化
                    inside_code -= 65248
                rstring += chr(inside_code)
            text = rstring
        else:
            rstring = ""
            for uchar in text:
                inside_code = ord(uchar)
                if inside_code == 32:  # 半角空格直接转化
                    inside_code = 12288
                elif inside_code >= 32 and inside_code <= 126:  # 半角字符（除空格）根据关系转化
                    inside_code += 65248
                rstring += chr(inside_code)
            text = rstring
        if self.CHINESE_SYMBOL:
            E_pun = u',.!?[]()<>"\''
            C_pun = u'，。！？【】（）《》“‘'
            table = {ord(f): ord(t) for f, t in zip(E_pun, C_pun)}
            text = text.translate(table)
        if self.SIMPLE == 1:
            text = Converter('zh-hans').convert(text)
        else:
            text = Converter('zh-hant').convert(text)
        return text