package temperature

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"

	"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/utils"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"

	"time"
)

type TempTable struct {
	Id          int64
	HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
	SensorID    int64
	Value       int64
}

var (
	timezoneName, timezoneOffset = time.Now().Zone()
	timezoneLocation = time.FixedZone(timezoneName, timezoneOffset)
	deltaTimeCompressData = 1440 * time.Hour
	log = logs.NewLogger(10000)
)

func init() {
	log.SetLogger(variables.LogType, variables.LogParams)
	orm.RegisterModel(new(TempTable))
	orm.DefaultRowsLimit = database.RowLimit
}

/**
 * Add the value into the database.
 */
func AddData(sensor, value int64) {
	log.Info("Add Temperature {sensor: %s, value: %d}", sensor, value)
	o := orm.NewOrm()
	o.Using(database.Alias)

	tmpTable := new(TempTable)
	tmpTable.Value = value
	tmpTable.SensorID = sensor
	o.Insert(tmpTable)
}

func AddDataWithHorodate(id, sensor, value int64, horodateGMT time.Time) {
		log.Info("Add Temperature {sensor: %s, value: %d, horodateGNT: %v}", sensor, value, horodateGMT)
		o := orm.NewOrm()
		o.Using(database.Alias)

		tmpTable := new(TempTable)
		tmpTable.Id = id
		tmpTable.Value = value
		tmpTable.SensorID = sensor
		tmpTable.HorodateGMT = horodateGMT
		_, err := o.Insert(tmpTable)
		if err != nil {
			log.Info("Error on insert temperature ", err)
		}
}

func GetAllTempForSensor(sensorId int64) []TempTable {
	o := orm.NewOrm()
	o.Using(database.Alias)

	var dataArray []TempTable
	var maps []orm.Params
	_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).Values(&maps)
	if err == nil {
		for _, m := range maps {
			d := new(TempTable)
			d.Id = utils.GetInt(m, "Id")
			d.HorodateGMT = utils.GetTime(m, "HorodateGMT")
			d.SensorID = utils.GetInt(m, "SensorID")
			d.Value = utils.GetInt(m, "Value")

			dataArray = append(dataArray, *d)
		}
	}
	return dataArray
}

func GetLastTempForSensor(sensorId int64) *TempTable {
	o := orm.NewOrm()
	o.Using(database.Alias)

	data := new(TempTable)
	var maps []orm.Params
	_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).OrderBy("HorodateGMT").Values(&maps)
	if err == nil {
		for _, m := range maps {
			data.Id = utils.GetInt(m, "Id")
			data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
			data.SensorID = utils.GetInt(m, "SensorID")
			data.Value = utils.GetInt(m, "Value")
		}
	}
	return data
}

func DeleteTemperatureBySenor(sensorId int64) {
	log.Info("Delete temperatures for sensor id : %d", sensorId)
	o := orm.NewOrm()
	o.Using(database.Alias)

	temps := GetAllTempForSensor(sensorId)
	for _, temp := range temps {
		DeleteTemperatureById(temp.Id)
	}
}

func DeleteTemperatureById(tempId int64) {
		log.Info("Delete temperatures id : %d", tempId)
		o := orm.NewOrm()
		o.Using(database.Alias)
		o.Delete(&TempTable{Id: tempId})
}
