let daddy = angular.module("daddy", ["ngRoute", "ngAnimate", "ngCookies"])

let server = "http://127.0.0.1:7001"

daddy.controller("bodyCtrlr", function($scope, $cookies, stateData){

    $scope.state = stateData.state

    let user = $cookies.getObject('user')

    if (user == null) {
        stateData.logOut()
    } else {
        stateData.setUser(user)
    }
    
})

daddy.controller("headerCtrlr", function($scope, stateData){
        $scope.subtitle = "Inks Things."
})

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

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

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

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

    $scope.setGallery = () => {
        stateData.setPage("gallery")
        $location.path("/gallery")
    }

    $scope.setUpload = () => {
        stateData.setPage("gallery")
        $location.path("/gallery/upload")
    }

    $scope.setForum = () => {
        stateData.setPage("forum")
        $location.path("/forum")
    } 

    $scope.setTopicCreate = () => {
        stateData.setPage("forum")
        $location.path("/forum/create/topic")
    } 

    $scope.setCategoryCreate = () => {
        stateData.setPage("forum")
        $location.path("/forum/create/category")
    } 
        
    switch ($location.path()) {
        case "":
        case "/":
        case "/things":
            $scope.setThings()
            break
        case "/gallery":
            $scope.setGallery()
            break
        case "/gallery/upload":
            $scope.setUpload()
            break
        case "/forum":
            $scope.setForum()
            break
        case "/forum/topic":
                // this has to be done in the controller
            break
        case "/forum/create/topic":
            $scope.setTopicCreate()
            break
        case "/forum/category":
            // this has to be done in the controller
            break
        case "/forum/create/category":
            $scope.setCategoryCreate()
            break
        case "/login":
            $scope.setLogin()
            break
        case "/signup":
            $scope.setSignup()
            break
    }   

    $scope.clearGallery = () => {
        if (!window.confirm("Are you sure you want to remove all the images from the gallery?")) {
            return
        }
        console.log("removing all the piccys")
        $http({
            url : server + "/gallery/clear",
            method : "POST"
        }).then((res) => {
            console.log("clearererered?")
            if (res == 500) {
                console.log("we oopsed, server did a bungle: " + res.data)
            } else {
                console.log("yep.")
                stateData.state.content.gallery = []
            }
        })
    }

    $scope.logOut = () => {
        $cookies.remove('user')
        stateData.logOut()
    }

})

daddy.controller("thingsCtrlr", function($scope){
    $scope.links = JSON.parse(links /* from links.js */)
})

daddy.controller("petCtrlr", function($scope){

})

daddy.config(function ($routeProvider) {
    $routeProvider.when("/", {templateUrl : "views/things.html", controller : "thingsCtrlr"})
    $routeProvider.when("/things", {templateUrl : "views/things.html", controller : "thingsCtrlr"})
    $routeProvider.when("/pet", {templateUrl : "views/pet.html", controller : "petCtrlr"})
    $routeProvider.when("/gallery", {templateUrl : "views/gallery/gallery.html", controller : "galleryCtrlr"})
        $routeProvider.when("/gallery/upload", {templateUrl : "views/gallery/upload.html", controller : "uploadCtrlr"})
    $routeProvider.when("/forum", {templateUrl : "views/forum/forum.html", controller : "forumCtrlr"})
        $routeProvider.when("/forum/create/topic", {templateUrl : "views/forum/create_topic.html", controller : "createTopicCtrlr"})
        $routeProvider.when("/forum/topic/:topicNo", {templateUrl : "views/forum/topic.html", controller : "topicCtrlr"})
        $routeProvider.when("/forum/create/category", {templateUrl : "views/forum/create_category.html", controller : "createCategoryCtrlr"})
        $routeProvider.when("/forum/category/:catNo", {templateUrl : "views/forum/category.html", controller : "categoryCtrlr"})
    $routeProvider.when("/login", {templateUrl : "views/login.html", controller : "loginCtrlr"})
    $routeProvider.when("/signup", {templateUrl : "views/signup.html", controller : "signupCtrlr"})
    $routeProvider.otherwise({template : "<article><section class='item border'><h1>404 ... no clue fam ... try something else?</h1></section></article>"})
})

daddy.factory('stateData', function(){
    let state = {
        page : "things",
        title : "Things;",
        user : {
            userNo : null,
            userName : null,
            userLevel : 0
        },
        content : {
            gallery : null
        }
    }

    setPage = (newPage) => { 
        state.page = newPage 
        switch (state.page) {
            case "things":
                state.title = "Things;"
                break
            case "gallery":
                state.title = "Gallery;"
                break
            case "forum":
                state.title = "Forum;"
                break
        }
    }

    logOut = () => {
        state.user.userNo = null
        state.user.userName = null
        state.user.userLevel = 0
    }

    setUser = (user) => {
        state.user.userNo = user.userNo
        state.user.userName = user.userName
        state.user.userLevel = user.userLevel
    }

    return {
        state : state,
        setPage: setPage,
        logOut: logOut,
        setUser: setUser
    }
})

function getDate() {
    let date = new Date()    
    let out = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " - " + date.getHours() + ":" + date.getMinutes()
    return out
}

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email).toLowerCase());
}