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 {
  PutStockCarParameters,
  PutStockCarResponse,
  PutStockCarRequest
} 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 PutStockCarService extends ApiMethodBase<
  PutStockCarResponse,
  PutStockCarRequest,
  PutStockCarParameters
> {
  protected get method(): 'PUT' {
    return 'PUT';
  }

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

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

  /**
   * 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: { description: 'Объект `StockCar` сохранен' },
      request: {
        type: 'object',
        required: ['stockCar'],
        properties: {
          stockCar: { $ref: 'stock-cars.json#/components/schemas/StockCar' },
          stock: { $ref: 'stock-cars.json#/components/schemas/Stock' },
          stockGoalsControl: {
            $ref: 'stock-cars.json#/components/schemas/StockGoalsControl'
          },
          stockAppraisal: {
            $ref: 'stock-cars.json#/components/schemas/StockAppraisal'
          },
          similarCarSearch: {
            $ref: 'stock-cars.json#/components/schemas/SimilarCarSearch'
          }
        }
      },
      params: {
        type: 'object',
        required: ['id'],
        properties: { id: { type: 'integer' }, forceSave: { type: 'boolean' } },
        description: 'Model of parameters for API /stock-cars/{id}'
      }
    } as any;
  }

  /**
   * Mock data when servers is disabled (in DEV-mode).
   * @returns {any}
   */
  protected get mockData(): any {
    return {} 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);
  }
}
