package controllers

import (
	"github.com/astaxie/beego"

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

type UserController struct {
	beego.Controller
}

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

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

func (c *UserController) Get() {
	c.TplNames = "user.tpl"
}

func (c *UserController) Post() {
	pwd1 := c.Input().Get("password1")
	pwd2 := c.Input().Get("password2")

	if pwd1 == pwd2 {
		login := c.GetSession(variables.SessionName)
		switch lo := login.(type) {
		case string:
			user.ChangePassword(lo, pwd1)
		}
	}

	c.Redirect(variables.UserRoute, 302)
}
