daddy.controller("galleryCtrlr", function($scope, $http, $timeout, stateData){

    $scope.getPiccys = () => {
        console.log("getting the piccys")
        $http({
            url : server + "/gallery/all",
            method : "GET",
        }).then((res) => {
            console.log("is this a piccy?")
            if (res.status != 500) {
                $scope.error = false
                console.log(res)
                stateData.state.content.gallery = res.data.reverse()
            } else {
                $scope.error = true
                $scope.errorInfo = "Internal Server Error: " + res.data
            }
        })
    }

    $scope.makeComment = (piccy, text, maker) => {
        console.log("about to make comment")
        console.log($scope)

        let formData = new FormData()
        formData.append('Text', text)
        if (maker == null || maker == "") {
            formData.append('Maker', "Anonymous")
        } else {
            formData.append('Maker', maker)
        }
        formData.append('_id', piccy._id)

        $http({
            url : server + '/gallery/comment',
            method : 'POST',
            data : formData,
            headers : {'Content-Type' : undefined},
            transformRequest : angular.identity
            }).then((res) => {
                if (res.status == 500) {
                    console.log("server is broke: " + res.data)
                } else {
                    console.log("comment sent...")
                    console.log(res)
                }
        })
        $timeout(function() { $scope.getPiccys() }, 2000)
    }

})

daddy.controller("uploadCtrlr", function($scope, $http){
    $scope.currentTime = getDate()
    $scope.status = false

    let titles = {
        upload : "Upload a Picture!!",
        disclaimer : "Disclaimer!!"
    }

    $scope.titles = titles

    $scope.remind = (eula, valid) => {
        if (!eula) {
            window.alert("You must accept the disclaimer.")
        } else if (!valid) {
            window.alert("  You must upload an image.\nAccepted: JPEG, JPG, PNG, GIF.")
        }
    }

    $scope.uploadImage = (element) => {
        $scope.$apply(($scope) => {
            console.log("checking image")
            switch (element.files[0].type) {
                case "image/jpg":
                case "image/jpeg":
                case "image/png":
                case "image/gif":
                    console.log("valid file")
                    $scope.status = false
                    $scope.valid = true
                    $scope.files = element.files
                    break
                default:
                    console.log("invalid file")
                    $scope.status = true
                    $scope.valid = false
                    $scope.uploadStatus = "Invalid file type"
                    break
            }
        })
        console.log($scope.files)
    }

    $scope.addPiccy = () => {
        $scope.status = true
        if ($scope.piccy == undefined) {
            $scope.uploadStatus = "Invalid file type."
            return
        }

        $scope.uploadStatus = "Uploading image..."
        console.log("about to post")
        let formData = new FormData()
        formData.append('Title', $scope.piccy.Title)
        if ($scope.piccy.Author == null || $scope.piccy.Author == "") {
            formData.append('Author', "Anonymous")
        } else {
            formData.append('Author', $scope.piccy.Author)
        }
        formData.append('Submitted', getDate())
        formData.append('Image', $scope.files[0])

        $http({
            url : server + '/gallery/add',
            method : 'POST',
            data : formData,
            headers : {'Content-Type' : undefined},
            transformRequest : angular.identity
            }).then((res) => {
                console.log("piccy sent...");
                console.log(res)
                if (res.status = 201) {
                    $scope.uploadStatus = "Image Uploaded Successfully"
                } else if (res.status = 500) {
                    $scope.uploadStatus = "Image Upload Failed, Error: " + res.data
                }
        })            
    }
})
