"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessoriesService = void 0;
const node_path_1 = require("node:path");
const hap_client_1 = require("@homebridge/hap-client");
const common_1 = require("@nestjs/common");
const fs_extra_1 = require("fs-extra");
const node_cache_1 = __importDefault(require("node-cache"));
const config_service_1 = require("../../core/config/config.service");
const logger_service_1 = require("../../core/logger/logger.service");
let AccessoriesService = class AccessoriesService {
    constructor(configService, logger) {
        this.configService = configService;
        this.logger = logger;
        this.accessoriesCache = new node_cache_1.default({ stdTTL: 0 });
        if (this.configService.homebridgeInsecureMode) {
            this.hapClient = new hap_client_1.HapClient({
                pin: this.configService.homebridgeConfig.bridge.pin,
                logger: this.logger,
                config: this.configService.ui.accessoryControl || {},
            });
        }
    }
    async connect(client) {
        if (!this.configService.homebridgeInsecureMode) {
            this.logger.error('Homebridge must be running in insecure mode to control accessories.');
            return;
        }
        let services;
        const loadAllAccessories = async (refresh) => {
            if (!refresh) {
                const cached = this.accessoriesCache.get('services');
                if (cached && cached.length) {
                    client.emit('accessories-data', cached);
                }
            }
            services = await this.loadAccessories();
            this.refreshCharacteristics(services);
            client.emit('accessories-ready-for-control');
            client.emit('accessories-data', services);
            this.accessoriesCache.set('services', services);
        };
        await loadAllAccessories(false);
        const requestHandler = async (msg) => {
            if (msg.set) {
                const service = services.find(x => x.uniqueId === msg.set.uniqueId);
                if (service) {
                    try {
                        await service.setCharacteristic(msg.set.iid, msg.set.value);
                        services = await this.loadAccessories();
                        setTimeout(() => {
                            this.refreshCharacteristics(services);
                        }, 1500);
                    }
                    catch (e) {
                        client.emit('accessory-control-failure', e.message);
                    }
                }
            }
        };
        client.on('accessory-control', requestHandler);
        const monitor = await this.hapClient.monitorCharacteristics();
        const updateHandler = (data) => {
            client.emit('accessories-data', data);
        };
        monitor.on('service-update', updateHandler);
        const instanceUpdateHandler = async (data) => {
            client.emit('accessories-reload-required', services);
        };
        this.hapClient.on('instance-discovered', instanceUpdateHandler);
        const secondaryLoadTimeout = setTimeout(async () => {
            await loadAllAccessories(true);
        }, 3000);
        const onEnd = () => {
            clearTimeout(secondaryLoadTimeout);
            client.removeAllListeners('end');
            client.removeAllListeners('disconnect');
            client.removeAllListeners('accessory-control');
            monitor.removeAllListeners('service-update');
            monitor.finish();
            this.hapClient.removeListener('instance-discovered', instanceUpdateHandler);
        };
        client.on('disconnect', onEnd.bind(this));
        client.on('end', onEnd.bind(this));
        this.hapClient.refreshInstances();
    }
    refreshCharacteristics(services) {
        Promise.all(services.map(service => service.refreshCharacteristics().catch((error) => {
            this.logger.error(`Failed to refresh characteristics for service ${service.uniqueId}: ${error.message}`);
        }))).catch((error) => {
            this.logger.warn(`Failed to refresh characteristics: ${error.message}`);
        });
    }
    async loadAccessories() {
        if (!this.configService.homebridgeInsecureMode) {
            throw new common_1.BadRequestException('Homebridge must be running in insecure mode to access accessories.');
        }
        try {
            return await this.hapClient.getAllServices();
        }
        catch (e) {
            if (e.response?.status === 401) {
                this.logger.warn('Homebridge must be running in insecure mode to view and control accessories from this plugin.');
            }
            else {
                this.logger.error(`Failed to load accessories from Homebridge as ${e.message}.`);
            }
            return [];
        }
    }
    async getAccessory(uniqueId) {
        const services = await this.loadAccessories();
        const service = services.find(x => x.uniqueId === uniqueId);
        if (!service) {
            throw new common_1.BadRequestException(`Service with uniqueId of '${uniqueId}' not found.`);
        }
        try {
            await service.refreshCharacteristics();
            return service;
        }
        catch (e) {
            throw new common_1.BadRequestException(e.message);
        }
    }
    async setAccessoryCharacteristic(uniqueId, characteristicType, value) {
        const services = await this.loadAccessories();
        const service = services.find(x => x.uniqueId === uniqueId);
        if (!service) {
            throw new common_1.BadRequestException(`Service with uniqueId of '${uniqueId}' not found.`);
        }
        const characteristic = service.getCharacteristic(characteristicType);
        if (!characteristic || !characteristic.canWrite) {
            const types = service.serviceCharacteristics.filter(x => x.canWrite).map(x => `'${x.type}'`).join(', ');
            throw new common_1.BadRequestException(`Invalid characteristicType. Valid types are: ${types}.`);
        }
        if (['uint8', 'uint16', 'uint32', 'uint64'].includes(characteristic.format)) {
            value = Number.parseInt(value, 10);
            if (characteristic.minValue !== undefined && value < characteristic.minValue) {
                throw new common_1.BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`);
            }
            if (characteristic.maxValue !== undefined && value > characteristic.maxValue) {
                throw new common_1.BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`);
            }
        }
        if (characteristic.format === 'float') {
            value = Number.parseFloat(value);
            if (characteristic.minValue !== undefined && value < characteristic.minValue) {
                throw new common_1.BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`);
            }
            if (characteristic.maxValue !== undefined && value > characteristic.maxValue) {
                throw new common_1.BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`);
            }
        }
        if (characteristic.format === 'bool') {
            if (typeof value === 'string') {
                if (['true', '1'].includes(value.toLowerCase())) {
                    value = true;
                }
                else if (['false', '0'].includes(value.toLowerCase())) {
                    value = false;
                }
            }
            else if (typeof value === 'number') {
                value = value === 1;
            }
            if (typeof value !== 'boolean') {
                throw new common_1.BadRequestException('Invalid value. The value must be a boolean (true or false).');
            }
        }
        try {
            await characteristic.setValue(value);
            await service.refreshCharacteristics();
            return service;
        }
        catch (e) {
            throw new common_1.BadRequestException(e.message);
        }
    }
    async getAccessoryLayout(username) {
        try {
            const accessoryLayout = await (0, fs_extra_1.readJson)(this.configService.accessoryLayoutPath);
            if (username in accessoryLayout) {
                return accessoryLayout[username];
            }
            else {
                throw new Error('User not in Accessory Layout');
            }
        }
        catch (e) {
            return [
                {
                    name: 'Default Room',
                    services: [],
                },
            ];
        }
    }
    async saveAccessoryLayout(user, layout) {
        let accessoryLayout;
        try {
            accessoryLayout = await (0, fs_extra_1.readJson)(this.configService.accessoryLayoutPath);
        }
        catch (e) {
            accessoryLayout = {};
        }
        if (!await (0, fs_extra_1.pathExists)((0, node_path_1.join)(this.configService.storagePath, 'accessories'))) {
            await (0, fs_extra_1.mkdirp)((0, node_path_1.join)(this.configService.storagePath, 'accessories'));
        }
        accessoryLayout[user] = layout;
        (0, fs_extra_1.writeJsonSync)(this.configService.accessoryLayoutPath, accessoryLayout);
        this.logger.log(`Accessory layout changes saved for ${user}.`);
        return layout;
    }
    resetInstancePool() {
        if (this.configService.homebridgeInsecureMode) {
            this.hapClient.resetInstancePool();
        }
    }
};
exports.AccessoriesService = AccessoriesService;
exports.AccessoriesService = AccessoriesService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_service_1.ConfigService,
        logger_service_1.Logger])
], AccessoriesService);
//# sourceMappingURL=accessories.service.js.map