package wxbroadcastv2

import (
	"encoding/json"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/rds"
)

type Video struct {
	Index   int    `json:"index"`
	Img     string `json:"img"`
	Name    string `json:"name"`
	Name2   string `json:"name2"`
	Export  string `json:"export"`
	VideoId string `json:"video_id"`
	Star    bool   `json:"star"`
}

type VideoConfig struct {
	VideoList []*Video `json:"video_list"`
}

func (this *VideoConfig) Store() error {
	marshal, _ := json.Marshal(this)
	err := rds.Redis.Set(WXBROADCASTVIDEO, string(marshal), -1).Err()
	return err
}

func (this *VideoConfig) Load() error {
	val := rds.Redis.Get(WXBROADCASTVIDEO).Val()
	if val != "" {
		json.Unmarshal([]byte(val), this)
	}
	return nil
}

func (this *VideoConfig) LoadV2(channel, appid int32, reload bool) error {
	var val string
	if channel > 0 && appid > 0 {
		val = rds.Redis.HGet(WXBROADCASTVIDEOSPE, fmt.Sprintf("%v_%v", channel, appid)).Val()
	}

	if (reload && val == "") || (channel == 0 && appid == 0) { //如果是旧的或者是配置通用的
		val = rds.Redis.Get(WXBROADCASTVIDEO).Val()
	}
	if val != "" {
		json.Unmarshal([]byte(val), this)
	}
	return nil
}

func (this *VideoConfig) StoreV2(channel, appid int32) (err error) {

	marshal, _ := json.Marshal(this)
	if channel > 0 && appid > 0 {
		err = rds.Redis.HSet(WXBROADCASTVIDEOSPE, fmt.Sprintf("%v_%v", channel, appid), string(marshal)).Err()
	} else {
		err = rds.Redis.Set(WXBROADCASTVIDEO, string(marshal), -1).Err()
	}
	return err
}
