import { promises as fs } from 'fs';
import { dirname } from '../../app';
import fsSync from "fs";
import { autowired, reflection } from 'first-di';
import "reflect-metadata";
import { ComputeRepository } from './compute_repository';
import { ZipRepository } from './zip_repository';

@reflection
export class EntityRepository {

    @autowired()
    private readonly computedRepository: ComputeRepository;
    @autowired()
    private readonly zipRepository: ZipRepository;

    private path: String = dirname + '/public/'


    private getFileName(file: String) {
        return file.slice(0, file.indexOf('.'))
    }

    public async getDir(path) {
        return this._fullPath(await fs.readdir(path + ''), duplicatedDelete(this.path, path))
    }

    public isExistDirPath(path: String): boolean {
        return fsSync.existsSync(path + '')
    }

    public async saveRootEntity(buffer: Buffer, name: string) {
        const filePath = this.path + this.getFileName(name) + '/'

        if (this.isExistDirPath(filePath)) {
            await fs.rm(filePath, { recursive: true })
        }
        await fs.mkdir(filePath);
        await fs.writeFile(filePath + name, buffer);
        this.computedRepository.computedAdjaxedMatrix(filePath, filePath + name, this.getFileName(name))
    }

    public async getAllRootEntity() {
        return await fs.readdir('' + this.path)
    }

    public async getEntityStorage(entity: string): Promise<String[]> | undefined {
        return this._fullPath(await fs.readdir(this.path + entity), entity + '/')
    }

    private _fullPath(folderPath, helpElement = '') {
        return folderPath.map((el) => this.path + helpElement + el)
    }
    public async readJson<T>(path) {
        return JSON.parse((await fs.readFile(path)).toString())
    }
    public async saveGeration(data: Buffer, id: String) {
        const rootFolderPath = '' + this.path + id + '/'
        console.log(rootFolderPath)
        this.zipRepository.archive(rootFolderPath, data)
    }
    public computedStability(id: string, buildNumber: string) {
        const assemblyFolder = this.path + id + '/generation/'
        this.computedRepository.computedWriteStability(assemblyFolder, buildNumber, id)
    }
    public async saveStability(zip: Buffer, id:string, buildNumber:string) {
        const filePath = await this.zipRepository.archive(this.path as string, zip)
        // const buildNumber = data['buildNumber']
        const assemblyFolder = this.path + id + '/generation/stability/'

        if (!this.isExistDirPath(assemblyFolder)) {
            await fs.mkdir(assemblyFolder);
        }
        await this.zipRepository.archive(assemblyFolder as string, zip, buildNumber)
        fs.rmdir(filePath + '/', { recursive: true})

    }
}
function duplicatedDelete(strChild: String, strMain: String) {
    let result = ''
    for (let i = 0; i < strMain.length; i++) {
        if (!(strMain[i] === strChild[i])) {
            result += strMain[i]
        }
    }
    return result
} 