#!/usr/bin/env python3
# 使用系统默认的 python3 运行
###########################################################################################
# 作者:gfdgd xi
# 版本:1.4.3
# 更新时间:2021年9月11日
# 感谢:anbox、deepin 和 统信
# 基于 Python3 的 tkinter 构建
###########################################################################################
#################
# 引入所需的库
#################
import os
import threading
import time
import json
import ttkthemes
import subprocess
import updatekiller
import tkinter as tk
import tkinter.ttk as ttk

# 读取文本文档
def readtxt(path: "路径")->"读取文本文档":
    f = open(path, "r")  # 设置文件对象
    str = f.read()  # 获取内容
    f.close()  # 关闭文本对象
    return str  # 返回结果

###########################
# 程序信息
###########################
programPath = os.path.split(os.path.realpath(__file__))[0]  # 返回 string
information = json.loads(readtxt(programPath + "/information.json"))
programUrl = information["Url"][0]
version = information["Version"]
goodRunSystem = information["System"]
aaptVersion = subprocess.getoutput(f"'{programPath}/aapt/run-aapt.sh' version")
about = '''介绍        :一个基于 Python3 的 tkinter 制作的 UEngine 运行器,在新版本Deepin/UOS发布后,可以在应用商店安装部分官方已适配的安卓应用,对爱好者来说,不能自己安装APK软件包始终差点意思,本程序可以为Deepin/UOS上的UEngine安卓运行环境安装自定义APK软件包,并能发送安装的APK包启动菜单到桌面或系统菜单。

版本        :{}

适用平台    :{}

程序官网    :{}

©2021-{}'''.format(version, goodRunSystem, tk.TkVersion,  programUrl, time.strftime("%Y"))
tips = "\n".join(information["Tips"])
updateThingsString = "\n".join(information["Update"])
title = "UEngine 运行器 {}".format(version)
updateTime = information["Time"]
updateThings = "{} 更新内容:\n{}\n更新时间:{}".format(version, updateThingsString, updateTime, time.strftime("%Y"))
iconPath = "{}/runner.png".format(os.path.split(os.path.realpath(__file__))[0])
desktop = "/opt/apps/uengine-runner/UengineAndroidProgramList.desktop"
desktopName = "UengineAndroidProgramList.desktop"
contribute = "\n".join(information["Contribute"])
useProgram = ""
threading.Thread(target=useProgram).start()

# add sub window
#添加窗口开启关闭开关,防止重复开启
windowflag = "close"

def UseProgram():
    global useProgram
    useProgram = '''1、UEngine:{}
2、python3:{}
3、tkinter:{}
4、aapt:{}
5、dpkg:{}
6、mkdir:{}
7、echo
8、chmod:{}
9、adb:{}
10、deepin 终端:{}'''.format(subprocess.getoutput("uengine version"),
    subprocess.getoutput("python3 --version"),
    tk.TkVersion,
    subprocess.getoutput(f"'{programPath}/aapt/run-aapt.sh' version"),
    subprocess.getoutput("dpkg --version"),
    subprocess.getoutput("mkdir --version"),
    subprocess.getoutput("chmod --version"),
    subprocess.getoutput("adb version"),
    subprocess.getoutput("deepin-terminal -v"))

def showhelp():
     
    #define  window and frame and button label   
    # 
    global windowflag
    if windowflag == "close":
        helpwindow=tk.Tk()
        helpwindow.resizable(0, 0)
        helpwindow.title("帮助")
        

        # get screen width and height
        screen_width = helpwindow.winfo_screenwidth()
        screen_height = helpwindow.winfo_screenheight()
        # calculate position x and y coordinates  假设主窗口大小固定 570x236像素 ,设置窗口位置为屏幕中心。
        winwith=550
        winhigh=700
        x = (screen_width/2) - (winwith/2)
        y = (screen_height/2) - (winhigh/2)
        
        helpwindow.geometry("550x700"+"+{:.0f}+{:.0f}".format(x, y))
        helpwindow.iconphoto(False, tk.PhotoImage(file=iconPath))

        style = ttkthemes.ThemedStyle(helpwindow)
        style.set_theme("breeze")
        
        

        Frmroot=ttk.Frame(helpwindow)
        FrmMenu = ttk.Frame(Frmroot)
        FrmText = ttk.Frame(Frmroot)

        LabFrmText=ttk.LabelFrame(FrmText,text="帮助",height=800,borderwidth=3)  
        HelpStr = tk.StringVar() 
        HelpStr.set(tips)
        LabText = ttk.Label(LabFrmText, textvariable=HelpStr,width=55)
        LabText.config(wraplength=350)
        
        def on_closing():
            global windowflag
            windowflag = "close"
            print(windowflag)
            helpwindow.destroy()



        # define button func        
        def ChgLog():
            HelpStr.set(updateThingsString)
        def ChgAbout():
            HelpStr.set(about)
        def ChgDep():
            if useProgram == "":
                BtnZujian.configure(state=tk.DISABLED)
                return
            HelpStr.set(useProgram)
        def ChgCon():
            HelpStr.set(contribute)
        def ChgTips():
            HelpStr.set(tips)
            LabText.config(wraplength=350)

        BtnReadme = ttk.Button(FrmMenu, text="使用说明",width=14,command=ChgTips)
        BtnLog = ttk.Button(FrmMenu, text="更新内容",width=14,command=ChgLog)
        BtnZujian = ttk.Button(FrmMenu, text="程序依赖的组件",width=14,command=ChgDep)
        BtnGongxian = ttk.Button(FrmMenu, text="有贡献的开发者",width=14,command=ChgCon)
        BtnAbout = ttk.Button(FrmMenu, text="关于",width=14,command=ChgAbout)


        #layout
        FrmMenu.grid(row=0,column=0,sticky=tk.NW)
        BtnReadme.grid(row=0,column=0,sticky=tk.NW,padx=3)
        BtnLog.grid(row=1,column=0,sticky=tk.NW,padx=3)
        BtnZujian.grid(row=2,column=0,sticky=tk.NW,padx=3)
        BtnGongxian.grid(row=3,column=0,sticky=tk.NW,padx=3)
        BtnAbout.grid(row=4,column=0,sticky=tk.NW,padx=3)

        FrmText.grid(row=0,column=1,sticky=tk.NW)
        LabFrmText.grid(row=0,column=0,sticky=tk.NW,padx=3,pady=3)
        LabText.grid(row=0,column=0,sticky=tk.NW)
      
        Frmroot.pack()
        windowflag = "open"
        print(windowflag)
        helpwindow.mainloop()
        #helpwindow.protocol("WM_DELETE_WINDOW", on_closing)
showhelp()