package clt_client

import (
	"encoding/json"
	"github.com/sirupsen/logrus"
	"lc/common/util"
	"os"
	"path"
)

var textVsn TextVsn

type ProgramInfo struct {
	Type      string    `json:"type"`      //节目类型
	Name      string    `json:"name"`      //节目名
	ImageInfo ImageInfo `json:"imageinfo"` //图片,视频资源url
	TextInfo  TextInfo  `json:"textinfo"`  //文本内容
}
type TextInfo struct {
	Content    string `json:"content"`
	LfFaceName string `json:"lfFaceName"`
	BackColor  string `json:"backcolor"`
	TextColor  string `json:"textcolor"`
	Duration   string `json:"duration"`
	IsScroll   string `json:"IsScroll"`
	Speed      string `json:"Speed"` //滚动速度pixels/秒
}
type ImageInfo struct {
	Urls     []string `json:"urls"`
	Duration string   `json:"duration"`
	InEffect InEffect `json:"inEffect"`
}

// ListResp 查所有节目的响应结构体(只包含当前播放节目)
type ListResp struct {
	Playing playing `json:"playing"`
}

type playing struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

// LoadVsn 加载  vsn文件->结构体
func (c cltClient) LoadVsn() {
	text, _ := os.ReadFile(util.GetPath(0) + "text.vsn")
	json.Unmarshal(text, &textVsn)
}

func NewPVsn(pro ProgramInfo) []byte {
	var marshal []byte
	switch pro.Type {
	case "2":
		var v = NewVsn()
		v.Programs.Program.Pages[0].Regions[0].Items[0].Type = "2"
		v.Programs.Program.Pages[0].Regions[0].Items[0].FileSource.FilePath = ".\\" + pro.Name + ".files\\" + path.Base(pro.ImageInfo.Urls[0])
		v.Programs.Program.Pages[0].Regions[0].Items[0].FileSource.IsRelative = "1"
		items := v.Programs.Program.Pages[0].Regions[0].Items
		for i, va := range pro.ImageInfo.Urls {
			if i > 0 {
				item := Items{}
				item.Type = "2"
				item.FileSource.IsRelative = "1"
				item.FileSource.FilePath = ".\\" + pro.Name + ".files\\" + path.Base(va)
				if pro.ImageInfo.Duration != "" {
					item.Duration = pro.ImageInfo.Duration
				}
				if pro.ImageInfo.InEffect.Type != "" {
					item.InEffect.Type = pro.ImageInfo.InEffect.Type
				}
				if pro.ImageInfo.InEffect.Time != "" {
					item.InEffect.Time = pro.ImageInfo.InEffect.Time
				}
				items = append(items, item)
				v.Programs.Program.Pages[0].Regions[0].Items = items
			}
		}
		marshal, _ = json.Marshal(v)
	case "3":
		var v = NewVsn()
		v.Programs.Program.Pages[0].Regions[0].Items[0].Type = "3"
		v.Programs.Program.Pages[0].Regions[0].Items[0].Volume = "1.000000"
		v.Programs.Program.Pages[0].Regions[0].Items[0].FileSource.IsRelative = "1"
		v.Programs.Program.Pages[0].Regions[0].Items[0].ReserveAS = "0"
		v.Programs.Program.Pages[0].Regions[0].Rect.Width = "256"
		v.Programs.Program.Pages[0].Regions[0].Rect.Height = "256"
		v.Programs.Program.Pages[0].Regions[0].Items[0].FileSource.FilePath = ".\\" + pro.Name + ".files\\" + path.Base(pro.ImageInfo.Urls[0])
		marshal, _ = json.Marshal(v)
	case "4":
		fallthrough
	case "5":
		v := textVsn
		v.Programs.Program.Pages[0].Regions[0].Items[0].Type = "5"
		if pro.TextInfo.BackColor != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].BackColor = pro.TextInfo.BackColor
		}
		if pro.TextInfo.TextColor != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].TextColor = pro.TextInfo.TextColor
		}
		if pro.TextInfo.LfFaceName != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].LogFont.LfFaceName = pro.TextInfo.LfFaceName
		}
		if pro.TextInfo.Duration != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].MultiPicInfo.OnePicDuration = pro.TextInfo.Duration
		}
		if pro.TextInfo.IsScroll != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].IsScroll = pro.TextInfo.IsScroll
		}
		if pro.TextInfo.Speed != "" {
			v.Programs.Program.Pages[0].Regions[0].Items[0].Speed = pro.TextInfo.Speed
		}
		v.Programs.Program.Pages[0].Regions[0].Items[0].Text = pro.TextInfo.Content
		marshal, _ = json.Marshal(v)
	case "27":
		//v.Programs.Program.Pages[0].Regions[0].Items[0].Type = "27"
		//v.Programs.Program.Pages[0].Regions[0].Items[0].URL = pro.URL
		//v.Programs.Program.Pages[0].Name = pro.Name + ".vsn"
		//v.Programs.Program.Pages[0].AppointDuration = "3600000"
		//v.Programs.Program.Information.Width = "256"
		//v.Programs.Program.Information.Height = "256"
		//v.Programs.Program.Pages[0].Regions[0].Rect.Width = "128"
		//v.Programs.Program.Pages[0].Regions[0].Rect.Height = "128"
		//v.Programs.Program.Pages[0].LoopType = "1"
	default:
		logrus.Errorf("不支持的类型:%v", pro.Type)
		return nil
	}
	return marshal
}

// 图片/视频
// Vsn vsn文件结构体
type Vsn struct {
	Programs Programs `json:"Programs"`
}
type Rect struct {
	X           string `json:"X"`
	Y           string `json:"Y"`
	Width       string `json:"Width"`
	Height      string `json:"Height"`
	BorderWidth string `json:"BorderWidth"`
	BorderColor string `json:"BorderColor"`
}
type FileSource struct {
	IsRelative string `json:"IsRelative"`
	FilePath   string `json:"FilePath"`
}
type InEffect struct {
	Type string `json:"Type"` //0- No transition, 1-Random, 2-Left unveil, 3-Right unveil
	Time string `json:"DateTime"`
}
type Items struct {
	Type       string     `json:"Type"`
	Volume     string     `json:"Volume"`
	Duration   string     `json:"Duration"`
	FileSource FileSource `json:"FileSource"`
	ReserveAS  string     `json:"ReserveAS"`
	InEffect   InEffect   `json:"inEffect"`
	URL        string     `json:"URL"`
}
type Regions struct {
	Layer int     `json:"Layer"`
	Rect  Rect    `json:"Rect"`
	Items []Items `json:"Items"`
}
type Pages struct {
	Regions []Regions `json:"Regions"`
}
type Program struct {
	Pages []Pages `json:"Pages"`
}
type Programs struct {
	Program Program `json:"Program"`
}

func NewVsn() Vsn {
	return Vsn{Programs: Programs{
		Program: Program{
			Pages: []Pages{
				{Regions: []Regions{
					{Layer: 1,
						Rect: Rect{
							X:           "0",
							Y:           "0",
							Width:       "128",
							Height:      "256",
							BorderWidth: "0",
							BorderColor: "0xFFFFFF00",
						},
						Items: []Items{
							{
								Type:       "",
								FileSource: FileSource{},
							},
						},
					},
				}},
			},
		},
	}}
}

// 文本vsn
type TextVsn struct {
	Programs struct {
		Program struct {
			Pages []struct {
				Regions []struct {
					Rect struct {
						X      string `json:"X"`
						Y      string `json:"Y"`
						Width  string `json:"Width"`
						Height string `json:"Height"`
					} `json:"Rect"`
					Items []struct {
						Type         string `json:"Type"`
						BackColor    string `json:"BackColor"`
						TextColor    string `json:"TextColor"`
						Alpha        string `json:"Alpha"`
						BeGlaring    string `json:"BeGlaring"`
						MultiPicInfo struct {
							OnePicDuration string `json:"OnePicDuration"`
						} `json:"MultiPicInfo"`
						VerticalAlign int `json:"VerticalAlign"`
						CenteralAlign int `json:"CenteralAlign"`
						LogFont       struct {
							LfHeight    string `json:"lfHeight"`
							LfWeight    string `json:"lfWeight"`
							LfItalic    string `json:"lfItalic"`
							LfUnderline string `json:"lfUnderline"`
							LfStrikeOut string `json:"lfStrikeOut"`
							LfFaceName  string `json:"lfFaceName"`
						} `json:"LogFont"`
						RepeatCount    string `json:"RepeatCount"`
						Speed          string `json:"Speed"`
						IsScrollByTime string `json:"IsScrollByTime"`
						IsScroll       string `json:"IsScroll"`
						PlayLenth      string `json:"PlayLenth"`
						Text           string `json:"Text"`
					} `json:"Items"`
				} `json:"Regions"`
			} `json:"Pages"`
		} `json:"Program"`
	} `json:"Programs"`
}

type SSchedule struct {
	Sleep  string `json:"sleep"`
	Wakeup string `json:"wakeup"`
	Reboot string `json:"reboot"`
}
