import { Injectable } from '@angular/core'

import { Observable, of as observableOf } from 'rxjs'
import { map } from 'rxjs/operators'

// import { Apollo, gql } from 'apollo-angular'

import { Project } from '../admin-data.service'

export class AdminBasketFile {
    constructor(
        public id: number,
        public dir: string,
        public name: string,
        public url: string,
        public md5: string,
        public time: Date,
        public comment: string,
        public status: string,
        public store: string,
        public project: string,
        public surveyor: string,
        public equipment: string,
        public import_result?: string,
    ) {}
}

export class AdminBasket {
    constructor(
        public name: string,
        public files?: AdminBasketFile[],
        public columns?: string[],
        public uploadFields?: string[],
        public projects?: Project[],
    ) {}
}

export class BasketImportResult {
    constructor(
        public time: Date,
        public message: string,
        public details?: string,
    ) {}
}

export class AdminBasketUploadFieldData {
    constructor(
        public stores: string[],
        public statuses: string[],
        public projects: string[],
        public surveyors: string[],
        public equipments: string[],
    ) {}
}

// const getAdminBasketsQuery = gql`
// query admin_baskets {
//     admin_baskets {
//         name
//     }
// }
// `

// const getAdminBasketUploadFieldDataQuery = gql`
// query admin_basket_upload_field_data {
//     admin_basket_upload_field_data {
//         store
//         status
//         project
//         surveyor
//         equipment
//     }
// }
// `

// const getAdminBasketQuery = gql`
// query admin_basket ($name: String!) {
//     admin_basket (name: $name) {
//         name
//         files {
//             id
//             name
//             dir
//             url
//             md5
//             time
//             comment
//             status
//             store
//             project
//             surveyor
//             equipment
//         }
//         columns
//         uploadFields
//         projects
//     }
// }
// `

// const deleteAdminBasketItemMutation = gql`
// mutation deleteBasketItem ($basket: String!, $id: Int!) {
//     deleteBasketItem (basket: $basket, id: $id) {
//         result
//     }
// }
// `

// const importAdminBasketItemMutation = gql`
// mutation importBasketItem ($basket: String!, $id: Int!, $dryRun: Boolean) {
//     importBasketItem (basket: $basket, id: $id, dryRun: $dryRun) {
//         result {
//             message
//             time
//             details
//         }
//     }
// }
// `

@Injectable()
export class AdminBasketDataService {
    constructor(
        // private apollo: Apollo,
    ) {}

    getBaskets(): Observable<AdminBasket[]> {
        // Get the list a basket names
        console.warn('Migrate Graphql')
        return observableOf()
        // return this.apollo.query({
        //     query: getAdminBasketsQuery,
        // }).pipe(map(
        //     res => res['data']['admin_baskets'].map(
        //         (data: object) => new AdminBasket(
        //             data['name'],
        //         )
        //     )
        // ))
    }

    getBasketUploadFieldData(): Observable<AdminBasketUploadFieldData> {
        // Get the list a basket names
        console.warn('Migrate Graphql')
        return observableOf()
        // return this.apollo.query({
        //     query: getAdminBasketUploadFieldDataQuery,
        // }).pipe(map(
        //     res => res['data']['admin_basket_upload_field_data'].map(
        //         (data: object) => new AdminBasketUploadFieldData(
        //             data['store'],
        //             data['status'],
        //             data['project'],
        //             data['surveyor'],
        //             data['equipment'],
        //         )
        //     )
        // ))
    }

    getBasket(name: string): Observable<AdminBasket> {
        // Get all info and content of a basket
        console.warn('Migrate Graphql')
        return observableOf()
        // return this.apollo.query({
        //     query: getAdminBasketQuery,
        //     variables: {
        //         name: name
        //     }
        // }).pipe(map(
        //     res => {
        //         let data = res['data']['admin_basket']
        //         return new AdminBasket(
        //             data['name'],
        //             data['files'].map(file => new AdminBasketFile(
        //                 file['id'],
        //                 file['dir'],
        //                 file['name'],
        //                 file['url'],
        //                 file['md5'],
        //                 new Date(file['time']),
        //                 file['comment'],
        //                 file['status'],
        //                 file['store'],
        //                 file['project'],
        //                 file['surveyor'],
        //                 file['equipment'],
        //             )),
        //             data['columns'],
        //             data['uploadFields'],
        //             // XXX: the proejct id isn't actually used in the UI,
        //             //      but required in class definition
        //             data['projects'] && data['projects'].map(
        //                 (projectName: string) => new Project(undefined, projectName)
        //             ),
        //         )
        //     }
        // ))
    }

    importItem(basket: string, id: number, dryRun: boolean=false): Observable<BasketImportResult> {
        console.warn('Migrate Graphql')
        return observableOf()
        // return this.apollo.mutate({
        //     mutation: importAdminBasketItemMutation,
        //     variables: {
        //         basket: basket,
        //         id: id,
        //         dryRun: dryRun
        //     }
        // }).pipe(map(
        //     resp => new BasketImportResult(
        //         resp['data']['importBasketItem']['result']['time'],
        //         resp['data']['importBasketItem']['result']['message'],
        //         JSON.parse(resp['data']['importBasketItem']['result']['details']),
        //     )
        // ))
    }

    deleteItem(basket: string, id: number) {
        console.warn('Migrate Graphql')
        return observableOf()
        // return this.apollo.mutate({
        //     mutation: deleteAdminBasketItemMutation,
        //     variables: {
        //         basket: basket,
        //         id: id,
        //     }
        // }).pipe(map(
        //     resp => resp['data']['deleteBasketItem']['result']
        // ))
    }
}