import { Injectable } from '@angular/core' ; import { Observable } from "rxjs/Observable" ; import { Http , RequestOptionsArgs , Headers , URLSearchParams , ResponseContentType } from "@angular/http" ; import { BaseService } from "./../base.service" ; import { PlanetaKinoV2City , PlanetaKinoV2Movie , PlanetaKinoV2Theater , PlanetaKinoTheater , PlanetaKinoV2Hall , PlanetaKinoV2Showtime , PlanetaKinoV2Login , PlanetaKinoV2Profile , } from './models' ; import { PlanetaKinoV2JsonResponse } from './json-response' ; import moment from 'moment' ; import * as _ from 'lodash' ; import { Store } from "@ngrx/store" ; import { State } from "./../../store" ; import * as selectors from './../../store/selectors' ; @ Injectable ( ) export class PlanetaKinoV2Service extends BaseService { private baseUrl = "http://cabinet.planetakino.ua/mapi/2" ; private citiesUrl = this . baseUrl + "/cities" ; private moviesUrl = this . baseUrl + "/movies" ; private theatersUrl = this . baseUrl + "/theaters" ; private hallSchemeUrl = this . baseUrl + "/hall-scheme" ; private movieDatesUrl = this . baseUrl + "/movie-dates" ; private showtimesUrl = this . baseUrl + "/showtimes" ; private baseUrl2 = "https://cabinet.planeta-kino.com.ua/mapiv2" ; private profileUrl = this . baseUrl2 + "/GetProfile" ; private loginUrl = this . baseUrl2 + "/Login" ; private logoutUrl = this . baseUrl2 + "/Logout" ; private sendActivationSmsUrl = this . baseUrl2 + "/SendActivationSMS" ; private registerUrl = this . baseUrl2 + "/register" ; private theatersAllUrl = "http://planetakino.ua/api/theatres" ; constructor ( http , private store ) { super ( http ) ; } getCities ( ) { return this . getData < any > ( this . citiesUrl ) . map ( res => { let data = res . cities . city ; return data ; } ) ; } getMovies ( data ) { let search = this . buildParams ( data ) ; return this . getData < any > ( this . moviesUrl , search , { cityId : data . cityId } ) . map ( res => { return { inTheaters : res . inTheaters . movie , soon : res . soon . movie } ; } ) ; } getTheatersAll ( ) { return super . getData < any > ( this . theatersAllUrl ) . map ( res => { return res . theatres . theatre ; } ) ; } getTheaters ( data ) { let search = this . buildParams ( { cityId : data . cityGroupId , } ) ; return this . getData < any > ( this . theatersUrl , search , { cityId : data . cityGroupId } ) . map ( res => { return res . theatres . theatre ; } ) ; } getHall ( data ) { let search = this . buildParams ( { showtimeId : data . showtimeId , theaterId : data . cinemaId , } ) ; return this . getData < any > ( this . hallSchemeUrl , search , { cityId : data . cityId } ) . map ( res => { return res . hall ; } ) ; } getMovieDates ( data ) { let search = this . buildParams ( { movieId : data . movieId , cityId : data . cityId , } ) ; return this . getData < any > ( this . movieDatesUrl , search , { cityId : data . cityId } ) . map ( res => { return res . showDates . showDate . map ( d => moment ( d ) . toDate ( ) ) ; } ) ; } getMovieShowtimes ( data ) { let search = this . buildParams ( { movieId : data . movieId , cityId : data . cityId , date : moment ( data . date ) . format ( "YYYY-MM-DD" ) , } ) ; return this . getData < any > ( this . showtimesUrl , search , { cityId : data . cityId } ) . map ( res => { return res . theaters . theater . showtimes . showtime ; } ) ; } login ( data ) { return this . postData < PlanetaKinoV2Login > ( this . loginUrl , { login : data . login , password : data . password , } ) ; } logout ( ) { return this . postData < PlanetaKinoV2JsonResponse < any > > ( this . logoutUrl , { } ) ; } getProfile ( ) { return this . postData < PlanetaKinoV2Profile > ( this . profileUrl , { ticketsinfo : "y" } ) ; } sendActivationSms ( data ) { return this . postData < { smsCode : string ; } > ( this . sendActivationSmsUrl , { phone : data . phone } ) ; } register ( data ) { return this . postData < any > ( this . registerUrl , { email : data . email , smsCode : data . smsCode , password : data . password , phone : data . phone , } ) ; } protected buildParams ( params ) { let res = new URLSearchParams ( ) ; Object . keys ( params ) . forEach ( key => { res . set ( key , params [ key ] ) ; } ) ; return res ; } protected getData < T > ( url , search ? , headers ? ) { headers = { ... headers } ; let options = { headers : new Headers ( headers ) , search : search , } ; return this . startWithAuthToken ( ) . switchMap ( authToken => { if ( ! _ . isEmpty ( authToken ) ) options . headers . append ( "Auth-Token" , authToken ) ; return super . getData < T > ( url , options ) . map ( res => { console . log ( 'planetakino-api:' , url , res ) ; return res ; } ) ; } ) ; } protected postData < T > ( url , data , headers ? ) { headers = { ... headers , "Content-Type" : "application/json" } ; let options = { headers : new Headers ( headers ) , responseType : ResponseContentType . Json , } ; return this . startWithAuthToken ( ) . switchMap ( authToken => { if ( ! _ . isEmpty ( authToken ) ) options . headers . append ( "Auth-Token" , authToken ) ; return super . postData < PlanetaKinoV2JsonResponse < T > > ( url , data , options ) . map ( res => { if ( res . code == "1" ) { return res ; } else { throw res ; } } ) ; } ) ; } protected startWithAuthToken ( ) { return this . store . select ( selectors . getAccountAuthToken ) . first ( ) ; } }