import { GenericCrudService } from '@d8d/shared-crud';
import { DataSource, Repository } from 'typeorm';
import { FeiePrinterMt } from '../entities/feie-printer.mt.entity';
import { FeieApiService } from './feie-api.service';
import { FeieApiConfig, PrinterDto, UpdatePrinterDto, PrinterStatus } from '../types/feie.types';

export class PrinterService extends GenericCrudService<FeiePrinterMt> {
  private feieApiService: FeieApiService;
  private feieConfig: FeieApiConfig;
  private printerRepository: Repository<FeiePrinterMt>;

  constructor(dataSource: DataSource, feieConfig: FeieApiConfig) {
    super(dataSource, FeiePrinterMt, {
      tenantOptions: { enabled: true, tenantIdField: 'tenantId' }
    });

    this.feieConfig = feieConfig;
    this.feieApiService = new FeieApiService(feieConfig);
    this.printerRepository = dataSource.getRepository(FeiePrinterMt);
  }

  /**
   * 添加打印机
   */
  async addPrinter(tenantId: number, printerDto: PrinterDto): Promise<FeiePrinterMt> {
    console.debug(`开始添加打印机，租户ID: ${tenantId}, 打印机SN: ${printerDto.printerSn}`);

    // 验证打印机配置
    const isValid = await this.feieApiService.validatePrinterConfig(
      printerDto.printerSn,
      printerDto.printerKey
    );

    if (!isValid) {
      console.error(`打印机配置验证失败，SN: ${printerDto.printerSn}, 租户ID: ${tenantId}`);
      throw new Error('打印机配置验证失败，可能原因：\n1. 打印机序列号或密钥错误\n2. 飞鹅API服务暂时不可用\n3. 网络连接问题\n请检查配置或稍后重试，如问题持续请联系管理员检查飞鹅API配置');
    }

    console.debug(`打印机配置验证通过，开始调用飞鹅API添加打印机`);
    // 调用飞鹅API添加打印机
    await this.feieApiService.addPrinter({
      sn: printerDto.printerSn,
      key: printerDto.printerKey,
      name: printerDto.printerName || ''
    });

    // 如果设置为默认打印机，先取消其他默认打印机
    if (printerDto.isDefault) {
      console.debug(`设置打印机为默认打印机，清除其他默认设置`);
      await this.clearDefaultPrinter(tenantId);
    }

    console.debug(`创建本地打印机记录，租户ID: ${tenantId}`);
    // 创建打印机记录
    const createData = {
      tenantId,
      printerSn: printerDto.printerSn,
      printerKey: printerDto.printerKey,
      printerName: printerDto.printerName,
      printerType: printerDto.printerType || '58mm',
      printerStatus: PrinterStatus.ACTIVE,
      isDefault: printerDto.isDefault ? 1 : 0
    };
    console.debug('创建数据:', JSON.stringify(createData, null, 2));

    const printer = await this.create(createData);

    console.debug(`打印机添加成功，ID: ${printer.id}`);
    return printer;
  }

  /**
   * 更新打印机
   */
  async updatePrinter(
    tenantId: number,
    printerSn: string,
    updateDto: UpdatePrinterDto
  ): Promise<FeiePrinterMt> {
    const printer = await this.printerRepository.findOne({
      where: { tenantId, printerSn }
    });

    if (!printer) {
      throw new Error('打印机不存在');
    }

    // 如果设置为默认打印机，先取消其他默认打印机
    if (updateDto.isDefault) {
      await this.clearDefaultPrinter(tenantId);
    }

    const updateData: Partial<FeiePrinterMt> = {};

    if (updateDto.printerName !== undefined) {
      updateData.printerName = updateDto.printerName;
    }

    if (updateDto.printerType !== undefined) {
      updateData.printerType = updateDto.printerType;
    }

    if (updateDto.printerStatus !== undefined) {
      updateData.printerStatus = updateDto.printerStatus;
    }

    if (updateDto.isDefault !== undefined) {
      updateData.isDefault = updateDto.isDefault ? 1 : 0;
    }

    const updatedPrinter = await this.update(printer.id, updateData);
    if (!updatedPrinter) {
      throw new Error('更新打印机失败');
    }
    return updatedPrinter;
  }

  /**
   * 删除打印机
   */
  async deletePrinter(tenantId: number, printerSn: string): Promise<void> {
    const printer = await this.printerRepository.findOne({
      where: { tenantId, printerSn }
    });

    if (!printer) {
      throw new Error('打印机不存在');
    }

    // 调用飞鹅API删除打印机
    await this.feieApiService.deletePrinter(printerSn);

    // 删除数据库记录
    await this.delete(printer.id);
  }

  /**
   * 获取打印机列表
   */
  async getPrinters(tenantId: number): Promise<FeiePrinterMt[]> {
    console.debug(`获取打印机列表，租户ID: ${tenantId}`);

    try {
      const printers = await this.printerRepository.find({
        where: { tenantId },
        order: { isDefault: 'DESC', createdAt: 'DESC' }
      });

      console.debug(`找到 ${printers.length} 台打印机`);
      if (printers.length > 0) {
        console.debug('打印机列表:', JSON.stringify(printers.map(p => ({
          id: p.id,
          tenantId: p.tenantId,
          printerSn: p.printerSn,
          printerName: p.printerName,
          printerStatus: p.printerStatus,
          isDefault: p.isDefault
        })), null, 2));
      }

      return printers;
    } catch (error) {
      console.error(`获取打印机列表失败，租户ID: ${tenantId}`, error);
      throw error;
    }
  }

  /**
   * 获取默认打印机
   */
  async getDefaultPrinter(tenantId: number): Promise<FeiePrinterMt | null> {
    return this.printerRepository.findOne({
      where: { tenantId, isDefault: 1 }
    });
  }

  /**
   * 查询打印机状态
   */
  async getPrinterStatus(tenantId: number, printerSn: string): Promise<any> {
    const printer = await this.printerRepository.findOne({
      where: { tenantId, printerSn }
    });

    if (!printer) {
      throw new Error('打印机不存在');
    }

    // 调用飞鹅API查询状态
    const status = await this.feieApiService.queryPrinterStatus(printerSn);

    // 更新本地状态
    const online = status.data && status.data.length > 0 ? status.data[0].online === 1 : false;
    const newStatus = online ? PrinterStatus.ACTIVE : PrinterStatus.INACTIVE;

    if (printer.printerStatus !== newStatus) {
      await this.update(printer.id, { printerStatus: newStatus });
    }

    return {
      ...status,
      localStatus: printer.printerStatus,
      isDefault: printer.isDefault === 1
    };
  }

  /**
   * 批量查询打印机状态
   */
  async batchGetPrinterStatus(tenantId: number): Promise<any[]> {
    const printers = await this.getPrinters(tenantId);
    const snList = printers.map(p => p.printerSn);

    if (snList.length === 0) {
      return [];
    }

    const status = await this.feieApiService.batchQueryPrinterStatus(snList);

    // 更新本地状态并返回结果
    const results = [];

    for (const printer of printers) {
      const remoteStatus = status.data?.find((s: any) => s.sn === printer.printerSn);
      const online = remoteStatus ? remoteStatus.online === 1 : false;
      const newStatus = online ? PrinterStatus.ACTIVE : PrinterStatus.INACTIVE;

      if (printer.printerStatus !== newStatus) {
        await this.update(printer.id, { printerStatus: newStatus });
      }

      results.push({
        printerSn: printer.printerSn,
        printerName: printer.printerName,
        printerType: printer.printerType,
        isDefault: printer.isDefault === 1,
        localStatus: newStatus,
        remoteStatus: remoteStatus || null,
        online
      });
    }

    return results;
  }

  /**
   * 清除默认打印机设置
   */
  private async clearDefaultPrinter(tenantId: number): Promise<void> {
    const defaultPrinter = await this.getDefaultPrinter(tenantId);

    if (defaultPrinter) {
      await this.update(defaultPrinter.id, { isDefault: 0 });
    }
  }

  /**
   * 设置默认打印机
   */
  async setDefaultPrinter(tenantId: number, printerSn: string): Promise<FeiePrinterMt> {
    const printer = await this.printerRepository.findOne({
      where: { tenantId, printerSn }
    });

    if (!printer) {
      throw new Error('打印机不存在');
    }

    // 清除其他默认打印机
    await this.clearDefaultPrinter(tenantId);

    // 设置新的默认打印机
    const updatedPrinter = await this.update(printer.id, { isDefault: 1 });
    if (!updatedPrinter) {
      throw new Error('设置默认打印机失败');
    }
    return updatedPrinter;
  }

  /**
   * 获取可用的打印机
   */
  async getAvailablePrinters(tenantId: number): Promise<FeiePrinterMt[]> {
    return this.printerRepository.find({
      where: {
        tenantId,
        printerStatus: PrinterStatus.ACTIVE
      },
      order: { isDefault: 'DESC', createdAt: 'DESC' }
    });
  }
}