daddy.controller("loginCtrlr", function($scope, $cookies, $location, $http, stateData){

    let titles = {
        login : "Login!",
        signup : "Not a Member?"
    }
    $scope.titles = titles

    let message = {
        start : "Feel free to",
        link : "Sign Up",
        end : "at any time!"
    }
    $scope.message = message

    $scope.setSignup = () => {
        stateData.setPage("things")
        $location.path("/signup")
    }

    $scope.login = () => {
        $scope.status = true
        $scope.loginStatus = "Logging you in..."
        console.log("logging in")        
        console.log("requesting authentication from server")
        $http({
            url : server + "/auth/login?userName="+$scope.user.username+"&userPass="+$scope.user.password,
            method : "GET"
        }).then((res) => {
            console.log(res)
            if (res.status == 200) {
                console.log("request granted")
                let today = new Date()
                let expired = new Date(today)
                expired.setDate(today.getDate()+1)
                $cookies.putObject('user', res.data[0], {expires : expired})
                console.log("set cookie")
                stateData.setUser(res.data[0])
                console.log("set data")
                stateData.setPage("things")
                $location.path("/things")
            }
        }).catch((res) => {
            if (res.status == 400) {
                $scope.loginStatus = "Incorrect information!"
                console.log("request denied")
            } else if (res.status == 500) {
                console.log("something went wrong: " + res)
            }
        })
    }

})

daddy.controller("signupCtrlr", function($scope, $http, $location, stateData) {

    let titles = {
        form : "Sign up here!",
        success : "Signup Success!"
    }
    $scope.titles = titles

    let message = {
        start : "You account has been created!",
        line2 : "Feel free to",
        link : "Login",
        end : "whenever you like."
    }
    $scope.message = message

    $scope.success = false
    $scope.exists = false
    $scope.status = false

    $scope.setLogin = () => {
        stateData.setPage("things")
        $location.path("/login")
    }
    
    $scope.signup = () => {
        $scope.userExists()
        $scope.passwordsMatch()
        $scope.emailGood()
        
        if ($scope.match && !$scope.exists && $scope.goodemail) {
            $scope.status = false
            $scope.submit()
        } else {
            $scope.status = true
            $scope.signupStatus = "Please correct errors above."
        }
    }

    $scope.emailGood = () => {
        if (validateEmail($scope.user.email)) {
            $scope.goodemail = true
            $scope.emailError = ""
        } else {
            $scope.goodemail = false
            $scope.emailError = "< invalid email."
        }
    }

    $scope.passwordsMatch = () => {
        if ($scope.user.password != $scope.user.confirmPassword) {
            $scope.match = false
            $scope.passwordError = "< password does not match."
        } else {
            $scope.match = true
            $scope.passwordError = ""
        }
    }

    $scope.submit = () => {
        $http({
            url : server + "/auth/signup?userName=" + $scope.user.username + "&userPass=" + $scope.user.password + "&userEmail=" + $scope.user.email,
            method : "POST"
        }).then((res) => {
            if (res.status == 200) {
                console.log("user created successfully")
                $scope.success = true
                $scope.signupForm.$setPristine()
                $scope.user.username = ""
                $scope.user.password = ""
                $scope.user.confirmPassword = ""
                $scope.user.email = ""
            }
        }).catch((res) => {
            if (res.status == 500) {
                console.log("whoops...")
            }
        })
    }

    $scope.userExists =  () => {
        console.log("checking username is used?")
        $http({
            url : server + "/auth/exists?userName=" + $scope.user.username,
            method : "POST"
        }).then((res) => {
            if (res.status == 200) {
                console.log(res.data)
                if (res.data == true) {
                    $scope.exists = true
                    $scope.usernameError= "< username already exists."
                } else {
                    $scope.exists = false
                    $scope.usernameError = ""
                }
            }
        }).catch((res) => {
            if (res.status == 500) {
                console.log ("something went wrong")
            }
        })
    }
    
})