"use strict";
app
    .filter("capitalize", () => {
        return (input) => {
            return (angular.isString(input) && input.length > 0) ? input.charAt(0).toUpperCase() + input.substr(1).toLowerCase() : input;
        };
    })
    .filter("titlecase", () => {
        return (input) => {
            input = input || "";
            return input.replace(/\w\S*/g, (txt) => {
                return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
            });
        };
    })
    .filter("datetz", () => {
        return (input, format) => {
            format = format ? format : 'DD-MM-YYYY hh:mm A';
            try {
                return input && input.length === 10 ? moment(input, 'DD-MM-YYYY').format(format) : input ? moment(input).format(format) : '';
            } catch (e) {
                console.error(e);
            }
        };
    })
    .directive("goBack", () => {
        return {
            restrict: "E",
            template: "<span class=\"pointer\" title=\"Back\" ><i class=\"fas fa-chevron-left \"></i></span>",
            scope: {},
            link: (scope, element, attrs) => {
                $(element[0]).on("click", () => {
                    history.back();
                    scope.$apply();
                });
            }
        };
    })
    .directive("emailLink", () => {
        return {
            restrict: "E",
            template: "<span class=\"email\" data-email=\"{{email}}\">{{email}}</span>",
            scope: {
                email: "@email",
            },
            link: (scope, element, attrs) => {
                $(element[0]).on("click", () => {
                    window.open("mailto:" + attrs.email, "popUpWindow", "height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no");
                    return false;
                });
            }
        };
    })
    .directive("datePicker", () => {
        return {
            restrict: "A",
            require: "ngModel",
            link: (scope, elm, attr, ctrl) => {
                // Format date on load
                ctrl.$formatters.unshift(value => {
                    try {
                        if (attr["default"] === '') {
                            return '';
                        } else if (attr["default"] === 'today') {
                            return moment().format("DD-MM-YYYY");
                        } else if (value && typeof value === "string" && value === "Invalid date") {
                            return moment().format("DD-MM-YYYY");
                        } else if (value && value.length === 10 && moment(value, "DD-MM-YYYY").isValid()) {
                            return moment(value, "DD-MM-YYYY").format("DD-MM-YYYY");
                        } else if (value && moment(value).isValid()) {
                            return moment(value).format("DD-MM-YYYY");
                        } else if (value) {
                            return value;
                        } else {
                            return moment().format('DD-MM-YYYY');
                        }
                    } catch (e) {
                        console.log("app/helper/filters.js", e)
                    }
                });
                //Disable Calendar
                scope.$watch(attr.ngDisabled, (newVal) => {
                    newVal ? $(elm).datepicker("disable") : $(elm).datepicker("enable");
                });

                // Datepicker Settings
                elm.focus(() => {
                    elm.datepicker("show");
                });
                elm.click(() => {
                    elm.datepicker("show");
                });

                elm.datepicker({
                    autoSize: true,
                    changeYear: true,
                    changeMonth: true,
                    yearRange:'-100:+0',
                    dateFormat: attr["dateformat"] || "dd-mm-yy",
                    showOn: "button",
                    buttonText: "<i class=\"fas fa-calendar \"></i>",
                    onSelect: (valu) => {
                        scope.$apply(() => {
                            ctrl.$setViewValue(valu);
                        });
                        elm.focus();
                    },
                    beforeShow: () => {
                        if (attr["minDate"] != null)
                            $(elm).datepicker("option", "minDate", attr["minDate"]);

                        if (attr["maxDate"] != null)
                            $(elm).datepicker("option", "maxDate", attr["maxDate"]);

                        if (attr["beforeshowdate"] != null) {
                            $(elm).datepicker("option", "beforeShowDay", (d) => {
                                var highlightedDates = JSON.parse(attr["beforeshowdate"]);
                                let day = moment(d).format('YYYY-MM-DD').toString();
                                if(highlightedDates.includes(day)){
                                    return [true,'highlight','Leave / Holiday on this day.'];
                                }else{
                                    return [true,'',''];
                                }
                            });
                        }
                    },
                });
            }
        };
    })
    .directive("datePicker2", () => {
        return {
            restrict: "A",
            require: "ngModel",
            link: (scope, elm, attr, ctrl) => {
                // Format date on load
                ctrl.$formatters.unshift(value => {
                    try {
                        if (attr["default"] === '') {
                            return '';
                        } else if (attr["default"] === 'today') {
                            return moment().format("DD-MM-YYYY");
                        } else if (value && typeof value === "string" && value === "Invalid date") {
                            return moment().format("DD-MM-YYYY");
                        } else if (value && value.length === 10 && moment(value, "DD-MM-YYYY").isValid()) {
                            return moment(value, "DD-MM-YYYY").format("DD-MM-YYYY");
                        } else if (value && moment(value).isValid()) {
                            return moment(value).format("DD-MM-YYYY");
                        } else if (value) {
                            return value;
                        } else {
                            return moment().format('DD-MM-YYYY');
                        }
                    } catch (e) {
                        console.log("app/helper/filters.js", e)
                    }
                });
                //Disable Calendar
                scope.$watch(attr.ngDisabled, (newVal) => {
                    newVal ? $(elm).datepicker("disable") : $(elm).datepicker("enable");
                });

                // Datepicker Settings
                elm.focus(() => {
                    elm.datepicker("show");
                });
                elm.click(() => {
                    elm.datepicker("show");
                });

                elm.datepicker({
                    autoSize: true,
                    changeYear: true,
                    changeMonth: true,
                    dateFormat: attr["dateformat"] || "dd-mm-yy",
                    showOn: "button",
                    buttonText: "<i class=\"fas fa-calendar \"></i>",
                    onSelect: (valu) => {
                        scope.$apply(() => {
                            ctrl.$setViewValue(valu);
                        });
                        elm.focus();
                    },
                    beforeShow: () => {
                        if (attr["minDate"] != null)
                            $(elm).datepicker("option", "minDate", attr["minDate"]);

                        if (attr["maxDate"] != null)
                            $(elm).datepicker("option", "maxDate", attr["maxDate"]);

                        if (attr["beforeshowdate"] != null && attr['beforeshowdate'] == 'mwf') {
                            $(elm).datepicker("option", "beforeShowDay", (d) => {
                                let day = d.getDay();
                                return [day == 1 || day == 3 || day == 5];
                            });
                        }
                    },
                });
            }
        };
    })
    .directive('fileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: (scope, element, attrs) => {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;

                element.bind('change', () => {
                    scope.$apply(() => {
                        if (attrs.multiple) {
                            modelSetter(scope, element[0].files);
                        } else {
                            modelSetter(scope, element[0].files[0]);
                        }
                    });
                });
            }
        };
    }])
    .directive("limitTo", [function () {
        //<input limit-to="4" type="number" class="form-control input-lg" ng-model="search.main" placeholder="">
        return {
            restrict: "A",
            link: (scope, elem, attrs) => {
                var limit = parseInt(attrs.limitTo);
                angular.element(elem).on("keypress", (e) => {
                    if (this.value.length === limit) e.preventDefault();
                });
                angular.element(elem).on("click", (e) => {
                    try {
                        this.select();
                    } catch (e) {
                        console.log(e);
                    }
                });
            }
        }
    }]);

$(document).ready(() => {
    $(document).on("click", ".toggle-fullscreen", (e) => {
        e.preventDefault();
        setTimeout(() => {
            let curObj = $(e.target);
            curObj = curObj.attr("class").indexOf("btn") === -1 ? curObj : curObj.next("i");
            //console.log(curObj.attr("class"));
            if (curObj.attr("class").indexOf("fa-expand") === -1) {
                curObj.addClass("fa-expand").removeClass("fa-compress");
                $(document).find('body').removeClass("fullscreen-section");
            } else {
                curObj.addClass("fa-compress").removeClass("fa-expand");
                $(document).find('body').addClass("fullscreen-section");
            }
            curObj.closest("div.card").toggleClass("panel-fullscreen", 10);
        }, 500);
    });
});
