package formview

import (
	"fmt"
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service/observer"
	"go_dreamfactory/comm"

	// "go_dreamfactory/modules/hero"
	"go_dreamfactory/pb"

	"fyne.io/fyne/v2"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
)

type SubWindowListView struct {
	ListBaseView
	title string
	winId string
	obs   observer.Observer
	w     fyne.Window
}

func NewSubWindowView(winId, title string, obs observer.Observer) *SubWindowListView {
	return &SubWindowListView{
		title: title,
		winId: winId,
		obs:   obs,
	}
}

func (this *SubWindowListView) createWindows() fyne.Window {
	this.w = fyne.CurrentApp().NewWindow(this.title)

	this.w.CenterOnScreen()
	this.w.SetFixedSize(true)
	this.w.Resize(fyne.Size{Width: 800, Height: 650})
	this.dataListener()
	return this.w
}

func (this *SubWindowListView) dataListener() {
	this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
		OnNotify: func(d interface{}, args ...interface{}) {
			data := d.(*pb.UserMessage)
			if !(data.MainType == string("hero") &&
				data.SubType == "hero.HeroSubTypeList") {
				return
			}

			rsp := &pb.HeroListResp{}
			if !comm.ProtoUnmarshal(data, rsp) {
				logrus.Error("unmarshal err")
			}

			this.itemListData = model.NewItemModelList()
			for _, v := range rsp.List {
				if v.CardType == 3 { //只显示类型是3的
					fm := &model.ItemModel{
						Id:    cast.ToString(v.Id),
						Label: fmt.Sprintf("%s Lv:%d S:%d T:%d", cast.ToString(v.HeroID), v.Lv, v.Star, v.CardType),
					}
					this.itemListData.DataList = append(this.itemListData.DataList, fm)
				}
			}
			this.reloadListData()
		},
	})
}
