package ui

import (
	"encoding/json"
	"errors"
	"fmt"
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/cmd/v2/lib/storage"
	"io/ioutil"
	"net/http"
	"time"

	mytheme "go_dreamfactory/cmd/v2/theme"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"github.com/sirupsen/logrus"
)

var toolWin *ToolWindowImpl

type ToolWindow interface {
	WindowInterface
}

type ToolWindowImpl struct {
	UIImpl
	parent fyne.Window
	w      fyne.Window
	tb     *toolBar      //工具条
	sb     *statusBar    //状态栏
	at     *appContainer //tabs
	mm     *toolMenu     //菜单
}

func NewToolWindow(ui *UIImpl, parent fyne.Window) ToolWindow {
	mw := &ToolWindowImpl{
		UIImpl: *ui,
		parent: parent,
	}

	toolWin = mw

	mw.sb = newStatusBar()

	toolbar := widget.NewToolbar(
		widget.NewToolbarAction(theme.ContentCopyIcon(), func() {
			openApp2(mw.at, common.TOOLBAR_GEN)
		}),

		widget.NewToolbarAction(theme.ContentAddIcon(), func() {
			openApp2(mw.at, common.TOOLBAR_PB)
		}),

		widget.NewToolbarAction(theme.DownloadIcon(), func() {
			openApp2(mw.at, common.TOOLBAR_SEC)
		}),

		widget.NewToolbarAction(theme.MailSendIcon(), func() {
			openApp2(mw.at, common.TOOLBAR_TERM)
		}),

		widget.NewToolbarAction(theme.ComputerIcon(), func() {
			openApp2(mw.at, common.TOOLBAR_PING)
		}),

		widget.NewToolbarAction(mytheme.ResourceMongodbJpg, func() {
			openApp2(mw.at, common.TOOLBAR_MGODB)
		}),

		widget.NewToolbarAction(mytheme.ResourceExcelJpg, func() {
			openApp2(mw.at, common.TOOLBAR_VALID)
		}),

		widget.NewToolbarSpacer(),
		widget.NewToolbarAction(theme.HelpIcon(), func() {
			showAbout()
		}),
	)

	mw.tb = newToolBar(toolbar)

	mw.at = newAppContainer(toolRegister, nil, ui.obs)

	return mw
}

func (ui *ToolWindowImpl) CreateWindow(title string, width, height float32, _ bool) {
	w := ui.app.NewWindow(fmt.Sprintf("工具箱 %s %s", title, ui.app.Metadata().Version))
	ui.AddWindow("tool", w)
	ui.w = w

	ui.mm = newToolMenu()
	ui.w.SetMainMenu(ui.mm.MainMenu)
	// content
	content := container.NewBorder(ui.tb.toolbar, ui.sb.widget,
		nil, nil, ui.at)
	ui.w.SetContent(content)
	appName, err := ui.at.openWelcome()
	if err != nil {
		logrus.WithField("appName", appName).Error(err)
	}
	w.Resize(fyne.NewSize(width, height))
	// w.SetMaster()

	w.Canvas().SetOnTypedKey(func(ke *fyne.KeyEvent) {
		switch ke.Name {
		case fyne.KeyEscape:
			{
				w.Close()
				ui.parent.Show()
			}
		}
	})
	w.CenterOnScreen()
	w.SetCloseIntercept(func() {
		ui.parent.Show()
		w.Close()
	})
	w.Show()
}
func (ui *ToolWindowImpl) quiteHandle() {
	dialog.ShowConfirm("提示", "确定退出吗", func(b bool) {
		if !b {
			return
		}
		ui.app.Quit()
	}, ui.w)
}

func (ui *ToolWindowImpl) syncConfig() {
	cli := http.Client{Timeout: time.Second * 10}
	r, err := cli.Get("http://10.0.0.9:8080/prd/config.json")
	if err != nil {
		logrus.Error(err)
		dialog.ShowError(err, ui.w)
		return
	}

	defer r.Body.Close()

	b, err2 := ioutil.ReadAll(r.Body)
	if err2 != nil {
		logrus.Error(err)
		dialog.ShowError(err2, ui.w)
		return
	}
	if len(b) == 0 {
		dialog.ShowError(errors.New("云配置可能不存在"), ui.w)
		return
	}
	config := &storage.Config{}
	if err := json.Unmarshal(b, config); err != nil {
		dialog.ShowError(fmt.Errorf("云配置解析错误: %s", err), ui.w)
		return
	}

	s, err := storage.NewOSStorage()
	s.StoreConfig(config)

	dialog.ShowConfirm("提示", "云配置同步成功,需要重新打开窗口", func(b bool) {
		if !b {
			return
		}
		ui.parent.Show()
		ui.w.Close()
	}, ui.w)
}
