import { Inject, Injectable } from '@nestjs/common';
import { ErrorService } from '../../error/error.service';
import { Client } from 'nestjs-soap';
import { ConfigurationService } from '../../config/configuration.service';
import { CarInfo, CustomerInfo, ProductItem } from './samt.interface';

@Injectable()
export class SamtService {
  constructor(
    @Inject('SAMT_SOAP_CLIENT') private readonly client: Client,
    private error: ErrorService,
    private config: ConfigurationService,
  ) {}

  /**
   * -------------------------------------
   */
  async GetOilAllocation(customer: CustomerInfo, car: CarInfo) {
    return new Promise((resolve, reject) => {
      const params = {
        input: {
          CustomerInfo: {
            MobileNo: customer.mobile,
            NationalCode: customer.nationalCode,
          },
          FleetInfoInput: {
            ChassisNo: car.vinCode,
            EngineNo: car.engineCode,
          },
          Password: this.config.thirdParty.samtBodyPassword,
          UserName: this.config.thirdParty.samtBodyUsername,
        },
      };

      console.log('GetOilAllocation', params);

      this.client.GetOilAllocation(params, (err, result) => {
        if (err)
          return reject({
            message: err || 'خطایی از سمت سیستم سازمان صمت رخ داده است',
            code: null,
          });

        if (result?.GetOilAllocationResult?.ResultCode !== 0) {
          return reject({
            message:
              result?.GetOilAllocationResult?.ResultMessage ||
              'خطایی از سمت سیستم سازمان صمت رخ داده است',
            code: result?.GetOilAllocationResult?.ResultCode || null,
          });
        }

        const data = result?.GetOilAllocationResult?.Obj;
        return resolve({
          allocation: data.Allocation || 0,
          fleetType: data.FleetType || 0,
        });
      });
    });
  }

  /**
   * -------------------------------------
   */
  async GetOilSaleOrder(
    customer: CustomerInfo,
    car: CarInfo,
    product: ProductItem,
  ) {
    return new Promise((resolve, reject) => {
      const params = {
        input: {
          CustomerInfo: {
            ChassisNo: car.vinCode,
            EngineNo: car.engineCode,
            MobileNo: customer.mobile,
            NationalCode: customer.nationalCode,
          },
          SaleInfo: {
            ProductCode: product.code,
            ProductPrice: `${product.price}`,
            SaleAmount: `${product.litr}`,
            SellerCommercialCode:
              this.config.thirdParty.samtSellerCommercialCode,
            SellerNationalCode: this.config.thirdParty.samtSellerNationalCode,
            PostalCode: this.config.thirdParty.samtSellerPostalCode,
          },
          UserName: this.config.thirdParty.samtBodyUsername,
          Password: this.config.thirdParty.samtBodyPassword,
        },
      };

      console.log('GetOilSaleOrder', params);

      this.client.GetOilSaleOrder(params, (err, result) => {
        if (err)
          return reject({
            message: err || 'خطایی از سمت سیستم سازمان صمت رخ داده است',
            code: null,
          });

        if (result?.GetOilSaleOrderResult?.ResultCode !== 0) {
          return reject({
            message:
              result?.GetOilSaleOrderResult?.ResultMessage ||
              'خطایی از سمت سیستم سازمان صمت رخ داده است',
            code: result?.GetOilSaleOrderResult?.ResultCode || null,
          });
        }

        const data = result?.GetOilSaleOrderResult?.Obj;
        return resolve({ trackingCode: data?.TrackingCode || null });
      });
    });
  }

  /**
   * -------------------------------------
   */
  async CancelOrderOilOrTire(trackingCode: string, buyCode: string) {
    return new Promise((resolve, reject) => {
      const params = {
        input: {
          Password: this.config.thirdParty.samtBodyPassword,
          TrackingCode: trackingCode,
          BuyCode: buyCode,
          Username: this.config.thirdParty.samtBodyUsername,
        },
      };

      console.log('CancelOrderOilOrTire', params);

      this.client.CancelOrderOilOrTire(params, (err, result) => {
        if (err) return reject(err);

        if (result?.CancelOrderOilOrTireResult?.ResultCode !== 0) {
          return reject(result?.CancelOrderOilOrTireResult?.ResultMessage);
        }

        return resolve(
          result?.CancelOrderOilOrTireResult?.ResultMessage || 'انجام شد',
        );
      });
    });
  }
}
