import { Injectable } from '@angular/core';

declare const JSZip: any;

@Injectable({
  providedIn: 'root'
})
export class ZipExportService {

  exportTemplates(files: any[]) {
    const zip = new JSZip();

    // Add all template files to the ZIP
    files.forEach(file => {
      zip.file(file.path, file.content);
    });

    // Add a README with instructions
    const readme = this.generateReadme();
    zip.file('README.md', readme);

    // Generate and download the ZIP file
    zip.generateAsync({ type: 'blob' })
      .then((content: Blob) => {
        this.downloadBlob(content, 'compodoc-templates.zip');
      });
  }

  private generateReadme(): string {
    return `# Compodoc Custom Templates

This ZIP file contains customized templates for Compodoc documentation generation.

## Contents

- **Templates** (\`.hbs\` files): Handlebars templates for generating documentation pages
- **Styles** (\`.css\` files): Stylesheets for customizing the appearance
- **Scripts** (\`.js\` files): JavaScript files for additional functionality

## Usage

1. Extract this ZIP file to a directory on your system
2. Use the \`--templates\` flag when running Compodoc to specify the path to your custom templates:

   \`\`\`bash
   compodoc -p tsconfig.json --templates ./path/to/custom/templates/
   \`\`\`

## Template Structure

- \`page.hbs\` - Main page template
- \`partials/\` - Directory containing partial templates
- \`styles/\` - Directory containing CSS files
- \`js/\` - Directory containing JavaScript files

## Customization Tips

1. **Templates**: Use Handlebars syntax to customize the HTML structure
2. **Styles**: Modify CSS to change colors, fonts, layout, etc.
3. **Scripts**: Add custom JavaScript functionality

## Backup

Always keep a backup of your original templates before making changes.

## Documentation

For more information about customizing Compodoc templates, visit:
https://compodoc.app/guides/template-customization.html

Generated by Compodoc Template Playground
`;
  }

  private downloadBlob(blob: Blob, filename: string) {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
  }
}
