namespace uniflash {
	export class DSDualCoreC28Impl extends DSDefaultImpl {
		private static deviceFilter = []; // [/.*F2837.*D/i, /.*F2847.*D/i];

		public static matches(partnum: string): boolean {
			for (let filter of DSDualCoreC28Impl.deviceFilter) {
				if (filter.test(partnum)) {
					return true;
				}
			}
			//nothing matches
			return false;
		}

		completeWorkflowAction(mainAction, coreName: string, optionObj: any) {
			this.$rootScope.$emit('ds.opStarted');

			let curCore;
			return this.configureDS(this.ufSession.getCCXML(), 'uniflash_temp.ccxml')
				.then((dsconfigObj) => {
					let coresMap = {};
					let matchingCoreName;
					angular.forEach(dsconfigObj.configData.cores, (core) => {
						if (core.indexOf('CLA') === -1) {
							coresMap[core] = this.connectToCore(core, dsconfigObj);
							if (core.indexOf(coreName) !== -1) {
								if (!matchingCoreName) {
									matchingCoreName = core;
								} else {
									throw 'core name "' + coreName + '" matches multiple cores on this device.';
								}
							}
						}
					});

					if (!matchingCoreName) {
						throw 'core name "' + coreName + '" does not match the cores on this device.';
					}
					return this.$q.all(coresMap)
						.then(() => {
							return coresMap[matchingCoreName];
						});
				})
				.then((core) => {
					if (core && mainAction) {
						curCore = core;
						return core.targetState.halt()
							.catch((e) => {
								if (e.message !== 'Target is not running') {
									this.$log.error('Halt target failed: ' + e.message);
									throw e;
								}
							});
					}
				})
				.then(() => {
					if (curCore) {
						return mainAction(curCore);
					}
				})
				.catch((error) => {
					if (error !== 'backdrop click' &&
						!(error instanceof ti_common.CancelError)) {
						return this.ticAlerts.showError(error.message);
					}
				})
				.finally(() => {
					return this.$q((resolve) => {
						if (curCore) {
							resolve(curCore.settings.get([])
								.then((ret) => {
									this.ufPropertiesValue.updateFromDS(ret.nameValueMap, this.ufContext.getCurrentCore().name);
								}));
						} else {
							resolve();
						}
					})
						.then(() => {
							if (optionObj && (optionObj.connect === false)) {
								// don't need to run target if we are not connecting to the target (ie; NCOperation)
								return;
							}
							if (curCore && optionObj && optionObj.runAfterOp) {
								return curCore.targetState.run(1).catch((e) => {
									if (e) {
										this.$log.error('completeWorkflowAction curCore.targetState.run(1) ' + JSON.stringify(e));
									}
								});
							}
						})
						.then(() => {
							if (optionObj && (optionObj.connect === false || optionObj.disconnect === false)) {
								// don't need to disconnect if we did not connect (ie; NCOperation), or if we don't want to disconnect
								return;
							}
							if (this.ufPreferencesSrv.get('Auto Disconnect')) {
								return this.disconnectAllCores().catch((e) => {
									if (e) {
										this.$log.error('completeWorkflowAction this.disconnectAllCores() ' + JSON.stringify(e));
									}
								});
							}
						})
						.finally(() => {
							this.$rootScope.$emit('ds.opFinished');
						});
				});

		}
	}
}