'use strict';

angular.module('history-detail', [])

    .controller('HistoryDetail', ['$scope', '$uibModalInstance', 'AppUtil', 'history', 'session', 'APIName',
        'util', 'Events', '_', 'app', '$', 'fileutil', 'moment', '$window', 'configServer',
        function ($scope, $uibModalInstance, AppUtil, history, session, APIName, util, Events, _, app, $,
                  fileutil, moment, $window, configServer) {

            $scope.historyInfo = history;

            // remove duplicate email for list
            $scope.historyInfo.data.result = arrUnique($scope.historyInfo.data.result);

            function arrUnique(arr) {
                var cleaned = [];
                arr.forEach(function (itm) {
                    var unique = true;
                    cleaned.forEach(function (itm2) {
                        if (_.isEqual(itm, itm2)) unique = false;
                    });
                    if (unique) cleaned.push(itm);
                });
                return cleaned;
            }

            $scope.currentUser = session.getUser();
            var EventProject = $scope.EventProject = Events.PROJECT;

            $scope.onClose = function () {
                $uibModalInstance.close();
            };

            $scope.getMemberName = function (item) {
                var name = item.user_first_name + " " + item.user_last_name;
                if (session.getCurrentLanguage() === 'ja') {
                    name = item.user_last_name + " " + item.user_first_name;
                }
                return name;
            };

            $scope.member_email = $scope.historyInfo !== null ? $scope.historyInfo.user_email : '';
            $scope.resultDetail = {
                member_action: $scope.getMemberName($scope.historyInfo) + " (" + $scope.member_email + ")",
            };

            // Get icon
            $scope.getIconFromMimeType = function (type) {
                return fileutil.getIconFromMimeType(type);
            };

            $scope.getShareLinkURL = function (link) {
                var linkUrl = null;
                if (!!link) {
                    linkUrl = configServer.FE_SECURE_MAIL_URL + "link-view-download/" + link.project_id + "/" + link.link_id;
                }
                return linkUrl;
            };

            $scope.isIn = function (value, list) {
                return _.contains(list, value);
            };

            $scope.positions = [
                {value: "diagonal", name: util.translate("watermark_position.diagonal")},
                {value: "center", name: util.translate("watermark_position.center")},
                {value: "top-left", name: util.translate("watermark_position.top-left")},
                {value: "top-center", name: util.translate("watermark_position.top-center")},
                {value: "top-right", name: util.translate("watermark_position.top-right")},
                {value: "bottom-left", name: util.translate("watermark_position.bottom-left")},
                {value: "bottom-center", name: util.translate("watermark_position.bottom-center")},
                {value: "bottom-right", name: util.translate("watermark_position.bottom-right")}];

            if ($scope.isIn($scope.historyInfo.history_type, [EventProject.SHARED_SECURE_MAIL.code])) {
                if ($scope.historyInfo.data && $scope.historyInfo.data.new_package) {
                    $scope.drmSetting = "reports.detail.yes";
                    $scope.printable = $scope.historyInfo.data.new_package && $scope.historyInfo.data.new_package.printable === 1 ? "reports.detail.yes" : "reports.detail.no";
                    $scope.editable = $scope.historyInfo.data.new_package && $scope.historyInfo.data.new_package.editable === 1 ? "reports.detail.yes" : "reports.detail.no";
                    $scope.expiry_date = $scope.historyInfo.data.new_package && $scope.historyInfo.data.new_package.document_expire !== null
                    && $scope.historyInfo.data.new_package.document_expire !== '9999-12-30T16:59:59Z' ?
                        util.getMoment($scope.historyInfo.data.new_package.document_expire).format("MM/DD/YYYY HH:mm:ss") + " (UTC)"
                        : "reports.detail.no";

                    var _wtm = JSON.parse($scope.historyInfo.data.new_package.watermark_setting);
                    $scope.watermarkSetting = _wtm === null || _wtm.show_wm === 0 ? "reports.detail.no" : "reports.detail.yes";
                    if (_wtm && _wtm.show_wm === 1) {
                        $scope.wtDefault = _wtm.default_wm === 1 ? "reports.detail.yes" : "reports.detail.no";
                        if (_wtm.default_wm == 1) {
                            $scope.wtText = $scope.currentUser.mail_address + " " + new Date().toLocaleDateString('en-US', {
                                month: "2-digit",
                                day: "2-digit",
                                year: "numeric"
                            });
                            $scope.wtFontSize = 18;
                            $scope.wtColor = 'RGB(255,0,0)';
                            $scope.wtPosition = _.find($scope.positions, {value: "diagonal"}).name;
                        } else {
                            $scope.wtText = _wtm.setting.wm_text;
                            $scope.wtFontSize = _wtm.setting.wm_font_size;
                            $scope.wtColor = _wtm.setting.wm_color;
                            $scope.wtPosition = _.find($scope.positions, {value: _wtm.setting.wm_position}).name;
                        }
                    }

                } else {
                    $scope.drmSetting = 'N/A';
                    $scope.watermarkSetting = 'N/A';
                }
            }

            $scope.getText = function (hItem) {
                // Detail of history
                var result = hItem.data && hItem.data.result,
                    length = (result) ? result.length : 0;

                switch (hItem.history_type) {
                    case EventProject.SHARED_SECURE_MAIL.code:
                        $scope.content = util.translate("reports.detail.share_secure_mail", {
                            "name": $scope.historyInfo.data.parent && $scope.historyInfo.data.parent.name !== null
                                ? $scope.historyInfo.data.parent.name : 'N/A'
                        });
                        break;

                    default:
                        break;
                }

                return $scope.content;
            };
            $scope.getText(history);
        }]);
