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 {
  GetStockAppraisalByCarParameters,
  GetStockAppraisalByCarResponse
} 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 GetStockAppraisalByCarService extends ApiMethodBase<
  GetStockAppraisalByCarResponse,
  null,
  GetStockAppraisalByCarParameters
> {
  protected get method(): 'GET' {
    return 'GET';
  }

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

  /**
   * 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: {
          stockAppraisal: {
            $ref: 'stock-cars.json#/components/schemas/StockAppraisal'
          },
          stockAppraisalCarSearch: {
            $ref: 'stock-cars.json#/components/schemas/StockAppraisalCarSearch'
          },
          dictionaryValueMeta: {
            type: 'object',
            properties: {
              stockAppraisal: {
                type: 'object',
                additionalProperties: {
                  type: 'array',
                  items: {
                    $ref:
                      'stock-cars.json#/components/schemas/DictionaryValueMeta'
                  }
                }
              },
              stockAppraisalCarSearch: {
                type: 'object',
                additionalProperties: {
                  type: 'array',
                  items: {
                    $ref:
                      'stock-cars.json#/components/schemas/DictionaryValueMeta'
                  }
                }
              },
              removedFields: {
                type: 'array',
                items: {
                  title:
                    'Список атрибутов `SimilarSearchCar`, которые были исключены из поиска.',
                  type: 'string'
                }
              }
            }
          }
        },
        example: {
          stockAppraisal: {
            id: null,
            complexEstimate: '3',
            complexMdsEstimate: '3',
            complexMdsDays: 34,
            complexSalesEstimate: '3+',
            complexAvailabilityEstimate: '3+',
            complexPublishedEstimate: '3',
            complexPublishedAmount: 34,
            complexOffersAmount: 11.109589041096,
            complexSalesAmount: 4.943023255814,
            complexSalesEstimatingPeriod: 60,
            complexPercent: 57,
            complexMdsPercent: 52,
            complexSalesPercent: 62,
            complexAvailabilityPercent: 63,
            sellingPrice: 620000,
            pendingPrice: null,
            pendingPriceSetAt: null,
            recommendedRetailPrice: null,
            buyoutPrice: null,
            repairCost: null,
            expenses: null,
            markup: null,
            markupPotential: 716000,
            profitPotentialDay: null,
            profitPotentialMonth: null,
            profitPotentialPercent: null,
            markupPercent: null,
            expensesPercent: null,
            marketPricesRating: 1,
            marketPricesMileageRating: 1,
            priceMileageCategory: 1,
            similarCarsTotal: 13,
            similarCarsAvgMillage: 105267,
            similarCarsMedianPrice: 716650,
            sellingPricePercent: 87,
            attentionPercent: null,
            salePeriod: 5,
            fullAttentionImpressionsPerDay: null,
            avgImpressionsPerDay: null,
            similarCarsAdjustedPrice: 715952,
            attentionRating: null,
            attentionCategory: null,
            priceCategory: 1,
            attentionSimilarCarsTotal: 1,
            geo: [1960671, 1573301],
            objectVersion: 0
          },
          stockAppraisalCarSearch: {
            region: {
              type: 'regions',
              regions: [
                {
                  id: 1960671,
                  type: {
                    id: 103,
                    type: 'city',
                    shortName: 'г',
                    fullName: 'Город'
                  },
                  name: 'Москва',
                  coordinates: [55.75396, 37.620393],
                  distance: null,
                  count: 140701
                },
                {
                  id: 1573301,
                  type: {
                    id: 105,
                    type: 'region',
                    shortName: 'обл',
                    fullName: 'Область'
                  },
                  name: 'Московская',
                  coordinates: [55.75496, 37.621393],
                  distance: null,
                  count: 46579
                }
              ],
              radius: { coordinates: [0, 0], radius: 0 }
            },
            creationYear: [2010, 2011, true, true],
            enginePower: [120, 180, true, true],
            engineVolume: [1.9, 2.1, true, true],
            bodyDoors: { body: 4, doors: 5 },
            brand: 7,
            model: 727,
            generation: [5014],
            vehicleType: null,
            engineType: 1,
            wheelType: 1,
            driveType: 1,
            gearType: 2,
            extendedModelsList: null,
            canExtendingByMixedModels: false,
            canExtendingByTwinsModels: false
          },
          dictionaryValueMeta: {
            stockAppraisal: {
              priceCategory: [{ value: 1, title: 'Очень низкая' }]
            }
          }
        },
        description:
          'Объект `StockAppraisal`, `StockAppraisalCarSearch` и `DictionaryValueMeta`'
      },
      request: null,
      params: {
        type: 'object',
        required: ['id'],
        properties: {
          id: { type: 'integer' },
          resolveDictionaries: { type: 'boolean' }
        },
        description: 'Model of parameters for API /stock-cars/{id}/appraisal'
      }
    } as any;
  }

  /**
   * Mock data when servers is disabled (in DEV-mode).
   * @returns {any}
   */
  protected get mockData(): any {
    return {
      stockAppraisal: {
        id: null,
        complexEstimate: '3',
        complexMdsEstimate: '3',
        complexMdsDays: 34,
        complexSalesEstimate: '3+',
        complexAvailabilityEstimate: '3+',
        complexPublishedEstimate: '3',
        complexPublishedAmount: 34,
        complexOffersAmount: 11.109589041096,
        complexSalesAmount: 4.943023255814,
        complexSalesEstimatingPeriod: 60,
        complexPercent: 57,
        complexMdsPercent: 52,
        complexSalesPercent: 62,
        complexAvailabilityPercent: 63,
        sellingPrice: 620000,
        pendingPrice: null,
        pendingPriceSetAt: null,
        recommendedRetailPrice: null,
        buyoutPrice: null,
        repairCost: null,
        expenses: null,
        markup: null,
        markupPotential: 716000,
        profitPotentialDay: null,
        profitPotentialMonth: null,
        profitPotentialPercent: null,
        markupPercent: null,
        expensesPercent: null,
        marketPricesRating: 1,
        marketPricesMileageRating: 1,
        priceMileageCategory: 1,
        similarCarsTotal: 13,
        similarCarsAvgMillage: 105267,
        similarCarsMedianPrice: 716650,
        sellingPricePercent: 87,
        attentionPercent: null,
        salePeriod: 5,
        fullAttentionImpressionsPerDay: null,
        avgImpressionsPerDay: null,
        similarCarsAdjustedPrice: 715952,
        attentionRating: null,
        attentionCategory: null,
        priceCategory: 1,
        attentionSimilarCarsTotal: 1,
        geo: [1960671, 1573301],
        objectVersion: 0
      },
      stockAppraisalCarSearch: {
        region: {
          type: 'regions',
          regions: [
            {
              id: 1960671,
              type: {
                id: 103,
                type: 'city',
                shortName: 'г',
                fullName: 'Город'
              },
              name: 'Москва',
              coordinates: [55.75396, 37.620393],
              distance: null,
              count: 140701
            },
            {
              id: 1573301,
              type: {
                id: 105,
                type: 'region',
                shortName: 'обл',
                fullName: 'Область'
              },
              name: 'Московская',
              coordinates: [55.75496, 37.621393],
              distance: null,
              count: 46579
            }
          ],
          radius: { coordinates: [0, 0], radius: 0 }
        },
        creationYear: [2010, 2011, true, true],
        enginePower: [120, 180, true, true],
        engineVolume: [1.9, 2.1, true, true],
        bodyDoors: { body: 4, doors: 5 },
        brand: 7,
        model: 727,
        generation: [5014],
        vehicleType: null,
        engineType: 1,
        wheelType: 1,
        driveType: 1,
        gearType: 2,
        extendedModelsList: null,
        canExtendingByMixedModels: false,
        canExtendingByTwinsModels: false
      },
      dictionaryValueMeta: {
        stockAppraisal: { priceCategory: [{ value: 1, 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);
  }
}
