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

let server = "https://www.inkletblot.com:32378"

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

    $scope.state = stateData.state
    
})

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

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

    $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")
    }
        
    switch ($location.path()) {
        case "":
        case "/":
        case "/things":
            $scope.setThings()
            break
        case "/gallery":
            $scope.setGallery()
            break
        case "/gallery/upload":
            $scope.setUpload()
            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 = []
            }
        })
    }

})

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.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;",
        content : {
            gallery : null
        }
    }

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

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

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