#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tkinter as tk
import tkinter.messagebox
import WeatherMail as wm
import re
import threading

win=tk.Tk()
win.title('Email Controller')
win.geometry('260x150+600+300')
win.resizable(width=False,height=False)

def rex_check():
    regex="^[a-zA-Z0-9]+[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]{2,6}$"
    result=re.findall(regex,Email_addr.get())
    return result


def clean_line(event):
    if Email_addr.get()=='请输入要发送的邮箱地址':
        Email_addr.set('')
    elif Email_addr.get()=='请务必输入正确的邮箱地址':
        Email_addr.set('')
    else:
        pass


def button1_disabled():
    Button1['state']='disabled'
    Button2['state']='disabled'
    Button3['state']='disabled'
    Stop_Button1['state']='normal'

def button2_disabled():
    Button1['state']='disabled'
    Button2['state']='disabled'
    Button3['state']='disabled'
    Stop_Button2['state']='normal'


def button3_disabled():
    Button1['state']='disabled'
    Button2['state']='disabled'
    Button3['state']='disabled'
    Stop_Button3['state']='normal'

def button1_enabled():
    Button1['state']='normal'
    Button2['state']='normal'
    Button3['state']='normal'
    Stop_Button1['state']='disabled'
    Stop_Button2['state']='disabled'
    Stop_Button3['state']='disabled'

def button2_enabled():
    Button1['state']='normal'
    Button2['state']='normal'
    Button3['state']='normal'
    Stop_Button1['state']='disabled'
    Stop_Button2['state']='disabled'
    Stop_Button3['state']='disabled'
    wm.change_coin()


def button3_enabled():
    Button1['state']='normal'
    Button2['state']='normal'
    Button3['state']='normal'
    Stop_Button1['state']='disabled'
    Stop_Button2['state']='disabled'
    Stop_Button3['state']='disabled'

def button1_engine():
    result=rex_check()
    if result==[]:
        tkinter.messagebox.showerror('抱歉','邮箱错误,请再次输入')
        Email_addr.set('请务必输入正确的邮箱地址')
    else:
        button1_disabled()
        wm.send_mail(result[0])
def button2_engine():
    result = rex_check()
    if result == []:
        tkinter.messagebox.showerror('抱歉','邮箱错误,请再次输入')
        Email_addr.set('请务必输入正确的邮箱地址')
    else:
        button2_disabled()
        t2_engine=threading.Thread(target=wm.run_progarm,args=(result[0],))
        t2_engine.start()

def button3_engine():
    wm.write_execl(wm.crawler()[1])
    tkinter.messagebox.showinfo('Succeed!','写入成功')

def button1_begin():
    t1=threading.Thread(target=button1_engine)
    t1.setDaemon(True)
    t1.start()
def button2_begin():
    t2=threading.Thread(target=button2_engine)
    t2.setDaemon(True)
    t2.start()
def button3_begin():
    t3=threading.Thread(target=button3_engine)
    t3.setDaemon(True)
    t3.start()

Email_addr=tk.StringVar(win,value='请输入要发送的邮箱地址')
Entry1=tk.Entry(win,width=20,textvariable=Email_addr)
Entry1.place(x=100,y=10)
Entry1.bind('<Button-1>',clean_line)

Label0=tk.Label(text='邮箱地址:')
Label0.place(x=60,y=20,anchor='center')

Label1=tk.Label(text='是否发送邮件')
Label1.place(x=40,y=50,anchor='w')
Label2=tk.Label(text='是否连续发送邮件')
Label2.place(x=40,y=80,anchor='w')
Label3=tk.Label(text='是否记录Excel')
Label3.place(x=40,y=110,anchor='w')
Label_version=tk.Label(text='Author lyb,   version 1.0')
Label_version.place(x=110,y=140,anchor='w')

Button1=tk.Button(win,text='开始',command=button1_begin)
Button1.place(x=160,y=50,anchor='center')
Button2=tk.Button(win,text='开始',command=button2_begin)
Button2.place(x=160,y=80,anchor='center')
Button3=tk.Button(win,text='开始',command=button3_begin)
Button3.place(x=160,y=110,anchor='center')

Stop_Button1=tk.Button(win,text='停止',state='disabled',command=button1_enabled)
Stop_Button1.place(x=200,y=50,anchor='center')
Stop_Button2=tk.Button(win,text='停止',state='disabled',command=button2_enabled)
Stop_Button2.place(x=200,y=80,anchor='center')
Stop_Button3=tk.Button(win,text='停止',state='disabled',command=button3_enabled)
Stop_Button3.place(x=200,y=110,anchor='center')

tk.mainloop()
