(function () {
    'use strict';
    angular.module('uniflash').service('ufAddOnTargetSrv', ufAddOnTargetSrv);
    ufAddOnTargetSrv.$inject = ['$window', 'ufTargetSetupSrv'];
    function ufAddOnTargetSrv($window, ufTargetSetupSrv) {
        var deviceList = [{ name: 'CC3100 / CC3200',
                family: 'Wireless',
                connection: 'Serial(UART) Interface',
                type: 'addon',
                subtype: 'v3',
                show: true,
                link: 'http://processors.wiki.ti.com/index.php/CCS_UniFlash_v3.4.1_Release_Notes#Installation_Instructions' },
            { name: 'CC31xx / CC32xx',
                family: 'Wireless',
                connection: 'Serial(UART) Interface',
                type: 'addon',
                subtype: 'desktop',
                show: true,
                link: 'http://processors.wiki.ti.com/index.php/Category:CCS_UniFlash#New_-_UniFlash_v4_now_available' }];
        var numFilteredDevices = 0;
        function getDeviceList() {
            if (ufTargetSetupSrv.selectedConnection && !ufTargetSetupSrv.selectedDevice) {
                return []; // if connection is selected but device is not
            }
            var filteredDeviceList = _.filter(deviceList, showDevice);
            numFilteredDevices = filteredDeviceList.length;
            return filteredDeviceList;
        }
        ;
        function getNumFilteredDevices() {
            return numFilteredDevices;
        }
        function showDevice(device) {
            return ((device.name.toLowerCase().indexOf(ufTargetSetupSrv.deviceFilter.toLowerCase()) !== -1) && device.show &&
                (ufTargetSetupSrv.deviceCategoryFilter === 'All' || ufTargetSetupSrv.deviceCategoryFilter === device.family));
        }
        ;
        function selectDevice(device) {
            ufTargetSetupSrv.selectedDevice = { id: device.name, type: device.type, subtype: device.subtype, link: device.link, moduleName: device.moduleName };
            ufTargetSetupSrv.selectedConnection = { id: device.connection };
            ufTargetSetupSrv.deviceSelected = true;
            ufTargetSetupSrv.lpSelected = false;
            ufTargetSetupSrv.hideDeviceSelectionOptions = true;
            ufTargetSetupSrv.hideConnectionSelectionOptions = true;
        }
        ;
        function handleAction() {
            if (ufTargetSetupSrv.selectedDevice.subtype === 'v3' || ufTargetSetupSrv.selectedDevice.subtype === 'desktop') {
                $window.open(ufTargetSetupSrv.selectedDevice.link, "_blank");
            }
            return true;
        }
        return {
            deviceList: deviceList,
            getDeviceList: getDeviceList,
            getNumFilteredDevices: getNumFilteredDevices,
            selectDevice: selectDevice,
            handleAction: handleAction
        };
    }
})();
