import { ImpressionCode } from '../../../protocol/impression/impressioncode' ; import { Metadata } from '../../../protocol/storage' ; import { ImpressionSender } from '../impressions/impressionsender' ; import { ModeManager } from '../mode/modemanager' ; import { PickerModel } from '../picker/pickermodel' ; import { PickerView } from '../picker/pickerview' ; import { ServerWrapper } from '../server/serverwrapper' ; export class PickerController { private impressionSender_ : ImpressionSender ; private server_ : ServerWrapper ; private modeManager_ : ModeManager ; private model_ : PickerModel | null ; private view_ : PickerView | null ; constructor ( impressionSender , server , modeManager ) { this . impressionSender_ = impressionSender ; this . server_ = server ; this . modeManager_ = modeManager ; this . model_ = null ; this . view_ = null ; } initialize ( model , view ) { this . model_ = model ; this . view_ = view ; } addMetadata ( ) { this . impressionSender_ . sendImpression ( ImpressionCode . PICKER_NEW_REPERTOIRE ) ; return this . server_ . createRepertoire ( ) . then ( newRepertoireId => { this . updatePicker ( ) . then ( ( ) => { this . selectMetadataId ( newRepertoireId ) ; this . modeManager_ . getSelectedMode ( ) . notifySelectedMetadata ( ) ; } ) ; } ) ; } selectMetadataId ( metadataId ) { if ( ! this . model_ || ! this . view_ ) { throw new Error ( 'Not initialized yet.' ) ; } if ( this . model_ . getSelectedMetadata ( ) . id == metadataId ) { return ; } this . impressionSender_ . sendImpression ( ImpressionCode . PICKER_SELECT_REPERTOIRE ) ; this . model_ . selectMetadataId ( metadataId ) ; this . view_ . refresh ( ) ; this . modeManager_ . getSelectedMode ( ) . notifySelectedMetadata ( ) ; } deleteMetadataId ( metadataId ) { if ( ! this . model_ || ! this . view_ ) { throw new Error ( 'Not initialized yet.' ) ; } this . impressionSender_ . sendImpression ( ImpressionCode . PICKER_DELETE_REPERTOIRE ) ; const notifyMode = metadataId == this . getSelectedMetadataId ( ) ; return this . server_ . deleteRepertoire ( metadataId ) . then ( ( ) => this . updatePicker ( ) ) . then ( ( ) => { if ( notifyMode ) { this . modeManager_ . getSelectedMode ( ) . notifySelectedMetadata ( ) ; } } ) ; } isModelEmpty ( ) { if ( ! this . model_ ) { throw new Error ( 'Not initialized yet.' ) ; } return this . model_ . isEmpty ( ) ; } getSelectedMetadataId ( ) { if ( ! this . model_ ) { throw new Error ( 'Not initialized yet.' ) ; } return this . model_ . getSelectedMetadata ( ) . id ; } updatePicker ( ) { if ( ! this . model_ || ! this . view_ ) { throw new Error ( 'Not initialized yet.' ) ; } const lastSelectedMetadataId = this . model_ . isEmpty ( ) ? null : this . model_ . getSelectedMetadata ( ) . id ; return this . server_ . getAllRepertoireMetadata ( ) . then ( metadataList => ! metadataList . length ? this . addMetadata ( ) . then ( ( ) => this . server_ . getAllRepertoireMetadata ( ) ) : Promise . resolve ( metadataList ) ) . then ( metadataList => this . populatePicker_ ( lastSelectedMetadataId , metadataList ) ) ; } private populatePicker_ ( lastSelectedMetadataId , metadataList ) { if ( ! this . model_ || ! this . view_ ) { throw new Error ( 'Not initialized yet.' ) ; } this . model_ . setMetadataList ( metadataList , lastSelectedMetadataId ) ; this . view_ . refresh ( ) ; } }