import { IGuideProgramDialogSettingStorageModel , NONE_ENCODE_OPTION } from '@/model/storage/guide/IGuideProgramDialogSettingStorageModel' ; import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../api' ; import { AudioComponentType , AudioSamplingRate , VideoComponentType } from '../../../lib/event' ; import DateUtil from '../../../util/DateUtil' ; import GenreUtil from '../../../util/GenreUtil' ; import IReservesApiModel from '../../api/reserves/IReservesApiModel' ; import IServerConfigModel from '../../serverConfig/IServerConfigModel' ; import IGuideProgramDialogState , { DisplayProgramData , ProgramDialogOpenOption , ProgramDialogReseveItem } from './IGuideProgramDialogState' ; @ injectable ( ) export default class GuideProgramDialogState implements IGuideProgramDialogState { public isOpen : boolean = false ; public displayData : DisplayProgramData | null = null ; public reserve : ProgramDialogReseveItem | null = null ; private reservesApiModel : IReservesApiModel ; private serverConfig : IServerConfigModel ; private setting : IGuideProgramDialogSettingStorageModel ; private programId : apid . ProgramId | null = null ; private program : apid . ScheduleProgramItem | null = null ; constructor ( @ inject ( 'IReservesApiModel' ) reservesApiModel , @ inject ( 'IServerConfigModel' ) serverConfig , @ inject ( 'IGuideProgramDialogSettingStorageModel' ) setting ) { this . reservesApiModel = reservesApiModel ; this . serverConfig = serverConfig ; this . setting = setting ; } param option public open ( option ) { this . isOpen = true ; this . program = option . program ; this . setProgramData ( option ) ; this . reserve = typeof option . reserve === 'undefined' ? null : option . reserve ; this . programId = option . program . id ; } /**
     * ダイアログを閉じる
     */ public close ( ) { this . isOpen = false ; this . displayData = null ; this . reserve = null ; this . programId = null ; } /**
     * ダイアログに表示するデータをセットする
     */ private setProgramData ( dialogData ) { const startAt = dialogData . program . startAt ; const endAt = dialogData . program . endAt ; const duration = Math . floor ( ( endAt - startAt ) / 1000 / 60 ) ; const result = { channelName : dialogData . channel === null ? dialogData . program . channelId . toString ( 10 ) : dialogData . channel . name , programName : dialogData . program . name , time : DateUtil . format ( DateUtil . getJaDate ( new Date ( startAt ) ) , 'MM/dd hh:mm ~ ' ) + DateUtil . format ( DateUtil . getJaDate ( new Date ( endAt ) ) , ' hh:mm' ) + `(${ duration . toString ( 10 ) }分)` , isFree : dialogData . program . isFree , } ; const genres = [ ] ; if ( typeof dialogData . program . genre1 !== 'undefined' ) { const genre = this . convertGenreToStr ( dialogData . program . genre1 , dialogData . program . subGenre1 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } if ( typeof dialogData . program . genre2 !== 'undefined' ) { const genre = this . convertGenreToStr ( dialogData . program . genre2 , dialogData . program . subGenre2 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } if ( typeof dialogData . program . genre3 !== 'undefined' ) { const genre = this . convertGenreToStr ( dialogData . program . genre3 , dialogData . program . subGenre3 ) ; if ( genre !== null ) { genres . push ( genre ) ; } } if ( genres . length > 0 ) { result . genres = genres ; } if ( typeof dialogData . program . description !== 'undefined' ) { result . description = dialogData . program . description ; } if ( typeof dialogData . program . extended !== 'undefined' ) { result . extended = dialogData . program . extended ; } if ( typeof dialogData . program . videoComponentType !== 'undefined' ) { const videoType = this . getVideoType ( dialogData . program . videoComponentType ) ; if ( videoType !== null ) { result . videoType = videoType ; } } if ( typeof dialogData . program . audioComponentType !== 'undefined' ) { const audioType = this . getAudioMode ( dialogData . program . audioComponentType ) ; if ( audioType !== null ) { result . audioType = audioType ; } } if ( typeof dialogData . program . audioSamplingRate !== 'undefined' ) { const audioSamplingRate = this . getAudioSamplingRate ( dialogData . program . audioSamplingRate ) ; if ( audioSamplingRate !== null ) { result . audioSamplingRate = audioSamplingRate ; } } this . displayData = result ; } param genre param subGenre private convertGenreToStr ( genre , subGenre ? ) { return GenreUtil . getGenres ( genre , subGenre ) ; } param videoComponentType return private getVideoType ( videoComponentType ) { const str = ( VideoComponentType as any ) [ videoComponentType ] ; return typeof str === 'undefined' ? null : str ; } param audioComponentType return private getAudioMode ( audioComponentType ) { const str = ( AudioComponentType as any ) [ audioComponentType ] ; return typeof str === 'undefined' ? null : str ; } return private getAudioSamplingRate ( audioSamplingRate ) { const str = AudioSamplingRate [ audioSamplingRate ] ; return typeof str === 'undefined' ? null : str ; } return public getProgramId ( ) { return this . programId ; } return public getProgram ( ) { return this . program ; } return public getEncodeList ( ) { const config = this . serverConfig . getConfig ( ) ; if ( config === null ) { return [ ] ; } const list = [ NONE_ENCODE_OPTION ] ; for ( const e of config . encode ) { list . push ( e ) ; } return list ; } param reserve public updateReserve ( reserve ) { this . reserve = reserve ; } /**
     * 簡易予約追加
     */ public async addReserve ( ) { if ( this . programId === null ) { throw new Error ( 'ProgramIdIsNull' ) ; } const option = { programId : this . programId , allowEndLack : true , } ; if ( this . setting . tmp . encode !== NONE_ENCODE_OPTION ) { option . encodeOption = { mode1 : this . setting . tmp . encode , isDeleteOriginalAfterEncode : this . setting . tmp . isDeleteOriginalAfterEncode , } ; } await this . reservesApiModel . add ( option ) ; } /**
     * 予約キャンセル
     */ public async cancelReserve ( ) { if ( this . reserve === null ) { throw new Error ( 'ReserveIsNull' ) ; } await this . reservesApiModel . cancel ( this . reserve . reserveId ) ; } /**
     * 予約除外状態の解除
     */ public async removeReserveSkip ( ) { if ( this . reserve === null ) { throw new Error ( 'ReserveIsNull' ) ; } await this . reservesApiModel . removeSkip ( this . reserve . reserveId ) ; } /**
     * 予約の重複状態を解除
     */ public async removeReserveOverlap ( ) { if ( this . reserve === null ) { throw new Error ( 'ReserveIsNull' ) ; } await this . reservesApiModel . removeOverlap ( this . reserve . reserveId ) ; } }