package windows

import (
	"bsphp/api"
	"net/url"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

// MainWindow 主窗口结构体
type MainWindow struct {
	app    fyne.App
	window fyne.Window
	api    api.BsPhp
}

// NewMainWindow 创建新的主窗口
func NewMainWindow(a fyne.App, api api.BsPhp) *MainWindow {
	w := a.NewWindow("主界面")
	w.Resize(fyne.NewSize(800, 600))

	// username, _ := main.appState.IsLoggedIn()

	mw := &MainWindow{
		app:    a,
		window: w,
		api:    api,
		// username: username,
	}

	// 设置主窗口内容
	mw.setupContent()
	return mw
}

// 设置主窗口内容
func (mw *MainWindow) setupContent() {
	// 设置描述区
	desc := widget.NewCard("软件详情:", "", widget.NewRichTextWithText(mw.api.GetSoftInfo()))

	statusItems := container.NewHBox(
		container.NewHBox(widget.NewLabel("到期时间: "+mw.api.GetEndTime()), widget.NewLabel("绑定特征:"+mw.api.GetBind())),
		layout.NewSpacer(),
		container.NewHBox(widget.NewLabel("服务器时间: "+mw.api.GetServerTime()), widget.NewLabel("版本号:"+mw.api.GetVersion())),
	)
	statusBar := widget.NewCard("", "", statusItems)

	button1 := widget.NewButton("网 络 测 试", func() {
		if mw.api.Connect() {
			dialog.NewInformation("网络测试:", "连接成功", mw.window).Show()
		} else {
			dialog.NewInformation("网络测试:", "连接失败", mw.window).Show()
		}
	})
	button2 := widget.NewButton("到 期 时 间", func() {
		dialog.NewInformation("到期时间:", mw.api.GetEndTime(), mw.window).Show()
	})
	button3 := widget.NewButton("取绑定特征", func() {
		dialog.NewInformation("绑定特征:", mw.api.GetBind(), mw.window).Show()
	})
	button4 := widget.NewButton("打开url", func() {
		url, _ := url.Parse(mw.api.GetURL())
		mw.app.OpenURL(url)
	})
	row1 := container.NewGridWithColumns(4, button1, button2, button3, button4)
	button5 := widget.NewButton("取用户信息", func() {
		userInfo := mw.api.GetUserInfo("信息字段：#激活时间>UserReDate#激活时Ip>UserReIp#用户状态>UserIsLock#登录时间>UserLogInDate#登录Ip>UserLogInIp#到期时>UserVipDate#绑定特征>UserKey#用户分组名称>Class_Nane#用户分组别名>Class_Mark##用户QQ>UserQQ#用户邮箱>UserMAIL#购卡折扣>UserPayZhe#是否代理代理>1>UserTreasury#电话>UserMobile#帐号金额>UserRMB#帐号积分>UserPoint#密保问题>Usermibao_wenti##VIP到期时间>UserVipDate#vip是否到期没有到期返回1到期返回2>UserVipWhether")
		dialog.NewInformation("用户信息:", strings.Replace(userInfo, "#", "\n", -1), mw.window).Show()
	})
	button6 := widget.NewButton("取验证数据", func() {
		dialog.NewInformation("验证数据:", mw.api.GetVerify(""), mw.window).Show()
	})
	button7 := widget.NewButton("取登录状态", func() {
		status := mw.api.GetLoginStatus()
		if status == "1" {
			dialog.NewInformation("登录状态:", "已登录", mw.window).Show()
		} else {
			dialog.NewInformation("登录状态:", "未登录", mw.window).Show()
		}
	})
	button8 := widget.NewButton("获取软件基本配置", func() {
		dialog.NewInformation("软件基本配置:", mw.api.GetSoftInfo(), mw.window).Show()
	})
	row2 := container.NewGridWithColumns(4, button5, button6, button7, button8)
	row := container.NewGridWithRows(2, row1, row2)

	content := container.NewBorder(
		desc, statusBar,
		nil, nil,
		row,
	)

	mw.window.SetContent(content)
}

// 显示主窗口
func (mw *MainWindow) Show() {
	mw.window.Show()
}
