$(function () {
    "use strict";
    BemsWebApplication.Factory.PointLocationMapping = {
        getDataSourceWithRelation: function(store, options) {
            return BWA.Factory.getBaseDataSourceWithRelation(
                store,
                options,
                this.getDataSourceOptions(),
                'FmsFacilityScheduleCheckGroup');
        },

        getDataSourceOptions: function() {
            return {
                select: [
                    'SiteId', 'FacilityTypeId', 'FacilityCode', 'PropertyId',
                    'BuildingId', 'FloorId', 'ZoneId',
                    'ValueType',
                    //'IsAccumulated',
                    'Name', 'Description',
                    'CmFacility/Name', 'BemsServiceType/Name', 'BemsFacilityType/Name', 'BemsFuelType/Name'
                ],
                expand: ['CmFacility', 'BemsServiceType', 'BemsFacilityType', 'BemsFuelType'],
                extendOptions: {
                    forceOriginalField: true

                    //alterNames: {
                    //    'CmFacility/Name': 'FacilityName',
                    //}
                },

                filter: [
                    ['SiteId', '=', BWA.UserInfo.SiteId()],
                    'and',
                    ['FacilityTypeId', '<', 100]
                ]
            };
        },

        getDataSource: function() {

            return BWA.DataUtil.createDataSource({

                dataSourceOptions: this.getDataSourceOptions()

            }, 'BemsMonitoringPoint')
        },

        getDataSourceWithLocation: function() {
            return BWA.DataUtil.createDataSource({
                dataSourceOptions: {
                    select: [
                        'SiteId', 'FacilityTypeId', 'FacilityCode', 'PropertyId',
                        'BuildingId', 'FloorId', 'ZoneId',
                        'ValueType',
                        //'IsAccumulated',
                        'Name', 'Description',
                        'CmFacility/Name', 'CmBuilding/Name', 'CmFloor/Name', 'CmZone/Name'
                    ],
                    expand: ['CmFacility', 'CmBuilding', 'CmFloor', 'CmZone'],
                    extendOptions: {
                        alterNames: {
                            'CmFacility/Name': 'FacilityName',
                            'CmBuilding/Name': 'BuildingName',
                            'CmFloor/Name': 'FloorName',
                            'CmZone/Name': 'ZoneName',
                        }
                    },
                    filter: [
                        ['SiteId', '=', BWA.UserInfo.SiteId()],
                        'and',
                        ['FacilityTypeId', '<', 100]
                    ]
                }
            }, 'BemsMonitoringPoint')
        },

        defaultColumns: [
            { dataField: 'CmFacility/Name', caption: $G('facilityName'), width: '25%', alignment: 'center' },
            { dataField: 'Name', caption: $G('pointName'), width: '30%', alignment: 'center' },
            {
                dataField: 'ValueType', caption: $G('valueType'), width: '10%', alignment: 'center', allowFilter: false,
                customizeText: function(cellInfo) {
                    return $G.pointValueType(cellInfo.value);
                }
            },
            //{
            //    dataField: 'IsAccumulated', caption: $G('accumulatedOrInstantaneous'), width: '15%', alignment: 'center', allowFilter: false,
            //    customizeText: function(cellInfo) {
            //        return cellInfo.value ? $G('accumulated') : $G('instantaneous');
            //    }
            //},
            { dataField: 'Location', caption: $G('location'), width: '35%', alignment: 'center' },
        ],
        
        cellPreparedFunc: function(cellElement, cellInfo) {
            if (cellInfo.rowType == 'data') {
                var data = cellInfo.data;
                switch (cellInfo.column.dataField) {
                    case 'Location':
                        {
                            $(cellElement).text($G.formatLocation(data.BuildingName(), data.FloorName(), data.ZoneName()));
                            break;
                        }
                }
            }
        },

    }
    $.extend(BWA.Factory.PointLocationMapping, BWA.DataUtil.pickFunctions(BWA.Factory));
})