import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { ApiMethodBase } from 'oapi3codegen-agent-angular/api-method.base';
import { ApiSchema } from 'oapi3codegen-agent-angular/api-schema';
import {
  SERVERS_DATA,
  ServersData
} from 'oapi3codegen-agent-angular/servers.data.provider';
import {
  API_ERROR_HANDLER,
  ApiErrorHandler
} from 'oapi3codegen-agent-angular/event-manager.provider';

// Typings for this API method
import { GetStockByCarParameters, GetStockByCarResponse } from '../typings';

import * as domainSchema from './stock-cars.json';

/**
 * Service for angular based on ApiAgent solution.
 * Provides assured request to API method with implicit
 * validation and common errors handling scheme.
 */
@Injectable()
export class GetStockByCarService extends ApiMethodBase<
  GetStockByCarResponse,
  null,
  GetStockByCarParameters
> {
  protected get method(): 'GET' {
    return 'GET';
  }

  /**
   * Path template, example: `/some/path/{id}`.
   * @returns {string}
   */
  protected get pathTemplate(): string {
    return '/stock-cars/{id}/stock';
  }

  /**
   * Parameters in a query.
   * @returns {string[]}
   */
  protected get queryParams(): string[] {
    return ['resolveDictionaries'];
  }

  /**
   * API servers.
   * @returns {string[]}
   */
  protected get servers(): string[] {
    return ['http://lk.cm.local/api/site/'];
  }

  /**
   * Complete domain API schema (OAS3) with library
   * of models.
   * @returns {SubSchema}
   */
  protected get domainSchema(): any {
    return domainSchema;
  }

  /**
   * JSON Schemas using for validations at requests.
   * @returns {Schema}
   */
  protected get schema(): ApiSchema {
    return {
      response: {
        type: 'object',
        properties: {
          stock: { $ref: 'stock-cars.json#/components/schemas/Stock' },
          dictionaryValueMeta: {
            type: 'object',
            properties: {
              stock: {
                type: 'object',
                additionalProperties: {
                  type: 'array',
                  items: {
                    $ref:
                      'stock-cars.json#/components/schemas/DictionaryValueMeta'
                  }
                }
              }
            }
          }
        },
        example: {
          stock: {
            id: 8487,
            stockState: 2,
            arrivedAt: '2016-12-01T00:00:00+03:00',
            outAt: '2018-07-13T20:51:20+03:00',
            saleAt: null,
            returnToSaleAt: null,
            contractType: null,
            inStockPeriod: 589,
            salesTacticId: 69,
            publishedDays: 590,
            publishStartedAt: '2016-12-01T00:00:00+03:00',
            publishFinishedAt: '2018-07-13T20:51:20+03:00',
            publishStatus: 2,
            lastPriceChangedAt: '2018-06-22T20:37:29+03:00',
            lastPriceChangedDays: 31,
            exposition: null,
            inChannel: null,
            outReason: null,
            certificated: null,
            saleStatus: 1,
            objectVersion: 0
          },
          dictionaryValueMeta: {
            stock: {
              stockState: [{ value: 2, title: 'Выбыл' }],
              saleStatus: [{ value: 1, title: 'В продаже' }],
              publishStatus: [{ value: 2, title: 'Не опубликован' }]
            }
          }
        },
        description: 'Объект `Stock` и `DictionaryValueMeta`'
      },
      request: null,
      params: {
        type: 'object',
        required: ['id'],
        properties: {
          id: { type: 'integer' },
          resolveDictionaries: { type: 'boolean' }
        },
        description: 'Model of parameters for API /stock-cars/{id}/stock'
      }
    } as any;
  }

  /**
   * Mock data when servers is disabled (in DEV-mode).
   * @returns {any}
   */
  protected get mockData(): any {
    return {
      stock: {
        id: 8487,
        stockState: 2,
        arrivedAt: '2016-12-01T00:00:00+03:00',
        outAt: '2018-07-13T20:51:20+03:00',
        saleAt: null,
        returnToSaleAt: null,
        contractType: null,
        inStockPeriod: 589,
        salesTacticId: 69,
        publishedDays: 590,
        publishStartedAt: '2016-12-01T00:00:00+03:00',
        publishFinishedAt: '2018-07-13T20:51:20+03:00',
        publishStatus: 2,
        lastPriceChangedAt: '2018-06-22T20:37:29+03:00',
        lastPriceChangedDays: 31,
        exposition: null,
        inChannel: null,
        outReason: null,
        certificated: null,
        saleStatus: 1,
        objectVersion: 0
      },
      dictionaryValueMeta: {
        stock: {
          stockState: [{ value: 2, title: 'Выбыл' }],
          saleStatus: [{ value: 1, title: 'В продаже' }],
          publishStatus: [{ value: 2, title: 'Не опубликован' }]
        }
      }
    } as any;
  }

  // *** Methods

  /**
   * @param {HttpClient} httpClient
   * @param {ApiErrorHandler} errorHandler
   * Обработчик ошибок HTTP-запросов и
   * @param {ServersData} serversData
   */
  constructor(
    protected httpClient: HttpClient,
    @Inject(API_ERROR_HANDLER) protected errorHandler: ApiErrorHandler,
    @Inject(SERVERS_DATA) protected serversData: ServersData
  ) {
    super(httpClient, errorHandler, serversData, domainSchema);
  }
}
