(function() {
    'use strict';

    angular.module('ti_common').service('ticFileDialogs', fileDialogs);

    fileDialogs.$inject = ['$q', 'ticFileStorage', 'ticFileFactory', '$uibModal', 'ticAlerts'];

    function fileDialogs($q, ticFileStorage: ti_common.file.ICloudStorage, ticFileFactory: ti_common.file.IFileFactory, $uibModal, ticAlerts) {


        var noFilesFoundMessage = 'No files available. To get started, upload a file using the dialog below.';
        // open an existing file
        function openFile(title: string, filter: RegExp, filterType: string): angular.IPromise<ti_common.file.File[]>;
        function openFile(title: string, extensions: string, filterType: string): angular.IPromise<ti_common.file.File[]>;
        function openFile(title: string, filter: any, filterType: string): angular.IPromise<ti_common.file.File[]> {

            return ticFileStorage.getFileList(filter).then(function(files: ti_common.file.File[]) {

                var modalInstance = $uibModal.open({
                    templateUrl: 'ti_common/cloud/open_file.html',
                    controllerAs: 'vm',
                    controller: OpenFileDialogController
                });

                function OpenFileDialogController() {
                    var vm = this;
                    vm.files = files;
                    vm.filter = filter;
                    vm.filterType = filterType;

                    vm.noFilesMessage = noFilesFoundMessage;
                    vm.title = title;

                    vm.getDefaultFiles = function() {
                        vm.files = files;
                    };

                    vm.getAllFiles = function() {
                        ticFileStorage.getFileList(/.*/)
                            .then(function(all_files: ti_common.file.File[]) {
                                 vm.files = all_files;
                            });
                    };

                    vm.filesSelected = function(uploads: ti_common.file.File[]) {
                        modalInstance.close(uploads);
                    };
                }

                return modalInstance.result;
            });
        }


        function saveFile(title: string, fileName: string, ext: string) {

            var modalInstance = $uibModal.open({
                templateUrl: 'ti_common/cloud/save_file.html',
                controllerAs: 'vm',
                controller: SaveFileDialogController
            });

            function SaveFileDialogController() {

                var vm = this;
                vm.title = title ? title : 'Enter File Name';
                vm.fileName = fileName ? fileName : 'untitled';
                vm.ext = ext ? ext : '';
                vm.saveFileClicked = saveFileClicked;

                function saveFileClicked(fileName) {
                    var fileNameWithExt = fileName + ext;
                    let file = ticFileFactory.create(fileNameWithExt);
                    return ticFileStorage.fileExists(fileNameWithExt).then(function(exists) {
                        if (!exists) {
                            return modalInstance.close(file);
                        }
                        else {
                            return ticAlerts.showConfirm('File Exists', 'File already exists! Overwrite ' + fileNameWithExt + '?').then(function() {
                                return modalInstance.close(file);
                            });
                        }
                    });
                }
            }

            return modalInstance.result;
        }

        return {
            openFile: openFile,
            saveFile: saveFile
        };
    }

})();