package deviceStatus

import (
	"github.com/gogf/gf/database/gredis"
)

var StatusKey = map[string]string{
	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:status:",
	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:status:",
	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:status:",
	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:status:",
	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:status:",
	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:status:",
	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:status:",
	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:status:",
	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:status:",
	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:status:",
}

var InfoKey = map[string]string{
	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:info:",
	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:info:",
	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:info:",
	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:info:",
	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:info:",
	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:info:",
	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:info:",
	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:info:",
	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:info:",
	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:info:",
}

var EventKey = map[string]string{
	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:event:",
	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:event:",
	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:event:",
	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:event:",
	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:event:",
	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:event:",
	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:event:",
	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:event:",
	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:event:",
	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:event:",
}

const (
	dataExpires = 7200
)

type SplitStatus struct {
	Power      int `json:"power"`
	Mode       int `json:"mode"`
	FanSpeed   int `json:"fan_speed"`
	SetTemp    int `json:"set_temp"`
	EnvTemp    int `json:"env_temp"`
	EnvCo2     int `json:"env_co2"`
	EnvPm25    int `json:"env_pm25"`
	StatusCode int `json:"status_code"`
	AirMode    int `json:"air_mode"`
	AcType     int `json:"ac_type"`
	AirType    int `json:"air_type"`
	HumType    int `json:"hum_type"`
}

type DevStatusManager struct {
	redisClient *gredis.Redis
}

func NewDevStatusManager(host string, port int) *DevStatusManager {
	red := gredis.New(&gredis.Config{
		Host:      host,
		Port:      port,
		Db:        14,
		MaxActive: 100,
	})
	mgr := &DevStatusManager{
		redisClient: red,
	}
	return mgr
}

func (mgr *DevStatusManager) GetDeviceStatus(id, productId string) (string, error) {
	key := StatusKey[productId] + id
	// get status from redis
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}

func (mgr *DevStatusManager) GetDeviceInfo(id, productId string) (string, error) {
	key := InfoKey[productId] + id
	// get status from redis
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}

func (mgr *DevStatusManager) GetDeviceStatusByKey(key string) (string, error) {
	// get status from redis
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}

func (mgr *DevStatusManager) GetWeatherInfo(location string) (string, error) {
	key := "weather:city:" + location
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}

func (mgr *DevStatusManager) SetWeatherInfo(location string, info string) error {
	key := "weather:city:" + location
	_, err := mgr.redisClient.DoVar("SET", key, info)
	if err != nil {
		return err
	}

	_, err = mgr.redisClient.Do("EXPIRE", key, 3600)
	if err != nil {
		return err
	}
	return nil
}

func (mgr *DevStatusManager) GetDeviceEvent(id, productId string) (string, error) {
	key := EventKey[productId] + id
	result, err := mgr.redisClient.DoVar("GET", key)
	if err != nil {
		return "", err
	}
	return result.String(), nil
}
