package controllers

import (
	"strconv"

	"github.com/astaxie/beego"

	"git.kingpenguin.tk/chteufleur/datahouse.git/models/notification"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/sensor"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/user"
	"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
)

type NotificationsController struct {
	beego.Controller
}

type NotificationView struct {
	Id        int64
	Condition string
	Value     int64
	Sensor    string
	Enable    bool
}

type SensorView struct {
	MAC  string
	Name string
}

func (c *NotificationsController) Prepare() {
	sess := c.GetSession(variables.SessionName)
	if sess == nil {
		c.Redirect(variables.LoginRouteNoRegex+variables.UserRoute, 302)
	} else {
		c.Data["IsAuthentificated"] = true
	}

	c.Data["IsNotifications"] = true
	c.Data["version"] = variables.Version
}

func (c *NotificationsController) Get() {
	var notifications []NotificationView
	var notifIds []int64
	u := c.getUserLogin()
	if u != nil {
		notif := notification.GetNotificationConditionByUser(u.Id)
		for _, nv := range notif {
			n := new(NotificationView)
			n.Id = nv.Id
			n.Condition = nv.Condition
			n.Value = nv.Value
			n.Enable = nv.Enable

			sensr := sensor.GetSensorByNotificationId(n.Id)
			if sensr != nil {
				n.Sensor = sensr.GetName()
				notifications = append(notifications, *n)
				notifIds = append(notifIds, n.Id)
			}
		}
	}
	c.Data["notifications"] = notifications

	var snsrs []SensorView
	sensors := sensor.GetAllSensor()
	for _, s := range sensors {
		snsrs = append(snsrs, SensorView{MAC: s.SensorMAC, Name: s.GetName()})
	}
	c.Data["sensors"] = snsrs

	c.TplName = "notifications.tpl"
}

func (c *NotificationsController) Post() {
	u := c.getUserLogin()

	action := c.Input().Get("action")
	notifIdStr := c.Input().Get("id")
	condition := c.Input().Get("condition")
	valueStr := c.Input().Get("value")
	sensMAC := c.Input().Get("sensor-mac")
	enableStr := c.Input().Get("enable")

	notifId, errNotifId := strconv.ParseInt(notifIdStr, 10, 64)
	value, errValue := strconv.ParseInt(valueStr, 10, 64)

	if action == "modif" && errNotifId == nil && errValue == nil {
		if notification.UpdateNotificationCondition(notifId, u.Id, condition, value, enableStr == "on") == nil {
			oldSensor := sensor.GetSensorByNotificationId(notifId)
			newSensor := sensor.GetSensorByMac(sensMAC)
			if oldSensor.Id != 0 && newSensor.Id != 0 && oldSensor.Id != newSensor.Id {
				oldSensor.DeleteNotification(notifId)
				newSensor.AddNotification(notifId)
			}
		}
	} else if action == "delete" && errNotifId == nil && errValue == nil {
		notification.DeleteNotificationCondition(notifId)
		sens := sensor.GetSensorByNotificationId(notifId)
		if sens.Id != 0 {
			sens.DeleteNotification(notifId)
		}
	} else if action == "add" && errValue == nil {
		sens := sensor.GetSensorByMac(sensMAC)
		if sens.Id != 0 {
			notifId, err := notification.AddNotificationCondition(u.Id, condition, value, enableStr == "on")
			if err == nil {
				sens.AddNotification(notifId)
			}
		}
	}

	c.Redirect(variables.NotificationsRoute, 302)
}

func (c *NotificationsController) getUserLogin() *user.User {
	ret := new(user.User)

	login := c.GetSession(variables.SessionName)
	switch lo := login.(type) {
	case string:
		ret = user.GetUserByLogin(lo)
	}

	return ret
}
