BWA.UserInfo = {

    IsCenter: ko.observable(false),
    IsLogin: ko.observable(false),
    SiteId: ko.observable(),
    UserId: ko.observable(),
    Name: ko.observable(),
    CompanyId: ko.observable(),
    CompanyName: ko.observable(),
    DepartmentId: ko.observable(),
    DepartmentName: ko.observable(),
    UserGroupName: ko.observable(), // 2016 07 27
    PositionId: ko.observable(),
    PositionName: ko.observable(),
    BusinessFieldId: ko.observable(),
    BusinessFieldName: ko.observable(), // 변경될 수 있으니 프로필 같은 곳으로 가면 갱신하자 
    MenuPermissions: ko.observableArray(),
    // hcLee 2018 02 23
    chartInfo: ko.observableArray(),
};

(function() {
    'use strict';

    var PERMISSION_SEARCH = 1,
        PERMISSION_MODIFICATION = 2,
        PERMISSION_DEPEND_BUSINESS_FIELD = 4
    ;

    var userInfo = BWA.UserInfo;

    function handleHasPermissionFunc(permissionIndex) {
        
        return function(menuViewId) {

            if (this.isAdmin()) {
                // Admin 계정은 업무분야와 상관없다.
                if (permissionIndex === PERMISSION_DEPEND_BUSINESS_FIELD) {
                    return false;
                }
                return true;
            }

            var menus = this.MenuPermissions();

            return _.some(menus, function(x) {

                var hasPermission = (x.MenuId === menuViewId) && ((x.MenuPermission & permissionIndex) > 0);
//                console.log('{0} : {1}, {2}, {3}'.formati(x.MenuId, menuViewId, x.MenuPermission, ((x.MenuPermission & permissionIndex) > 0)));
                return hasPermission;
            });

        }.bind(userInfo);
    }
    
    userInfo.hasPermissionOfSearch = handleHasPermissionFunc(PERMISSION_SEARCH);
    userInfo.hasPermissionOfModification = handleHasPermissionFunc(PERMISSION_MODIFICATION);
    userInfo.isDependBusinessField = handleHasPermissionFunc(PERMISSION_DEPEND_BUSINESS_FIELD);

    userInfo.isAdmin = function() {

        return this.UserId() === 'admin';

    }.bind(userInfo);

    // hcLee 2018 02 23
    userInfo.GetChartInfo = function (ftype, chartid) {
        /*
        _.each(this.chartInfo(), function (chart) {
            if (chart.FacilityTypeId() == ftype && chart.ChartId() == chartid) return chart;
        });
        return null;*/
        var chartinfos = this.chartInfo();
        for (var n = 0; n < chartinfos.length; n++) {
            if (chartinfos[n].FacilityTypeId() == ftype && chartinfos[n].ChartId() == chartid) return chartinfos[n];
        }
        return null;

    }.bind(userInfo);

    // hcLee 2018 02 26
    userInfo.GetChartInfo_Title = function (ftype, chartid) {
        /*
        _.each(this.chartInfo(), function (chart) {
            if (chart.FacilityTypeId() == ftype && chart.ChartId() == chartid) return chart.Title();
        });
        return '';*/

        var chartinfos = this.chartInfo();
        for (var n = 0; n < chartinfos.length; n++) {
            if (chartinfos[n].FacilityTypeId() == ftype && chartinfos[n].ChartId() == chartid) return chartinfos[n].Title();
        }
        return '';

    }.bind(userInfo);

    // hcLee 2018 02 27
    userInfo.GetChartInfo_Count = function (ftype) {
        var chartCount = 0;
        var chartinfos = this.chartInfo();
        for (var n = 0; n < maxLength; n++) {
            if (chartinfos[n].FacilityTypeId() == ftype)
                chartCount++;
        }
        return chartCount;
    }.bind(userInfo);

    userInfo.GetCharts = function (ftype) {
        var chartinfos = this.chartInfo();
        var charts = [];
        for (var n = 0; n < chartinfos.length; n++) {
            if (chartinfos[n].FacilityTypeId() == ftype)
                charts.push(chartinfos[n]);

            // hcLee 2018 04 23
            if (charts.length >= 17) return charts;
        }
        return charts;
    }.bind(userInfo);

    userInfo.logout = function() {

        this.IsCenter(false);
        this.IsLogin(false);

        this.SiteId(undefined);
        this.UserId(undefined);
        this.Name(undefined);
        this.CompanyId(undefined);
        this.CompanyName(undefined);
        this.BusinessFieldId(undefined);
        this.BusinessFieldName(undefined);
        this.DepartmentId(undefined);
        this.DepartmentName(undefined);
        this.UserGroupName(undefined);
        this.PositionId(undefined);
        this.PositionName(undefined);
        this.MenuPermissions(undefined);
        // hcLee 2018 02 23
        this.chartInfo(undefined);

    }.bind(userInfo);
})();