#!/usr/bin/env python
"""
convert_dfd_to_png.py - Converts DFD.html to a PNG image file
"""

import os
from pathlib import Path


def convert_html_to_png():
    # Define the input and output file paths
    input_file = "/Users/home/YandexDisk/TECHNOLYCEUM/ict/Year/2025/ai/ai7/ai7-m3/Thesis materials/DFD.html"
    output_file = "/Users/home/YandexDisk/TECHNOLYCEUM/ict/Year/2025/ai/ai7/ai7-m3/Thesis materials/DFD.png"
    
    # Check if the input file exists
    if not os.path.exists(input_file):
        print(f"Input file does not exist: {input_file}")
        return
    
    print("Converting DFD.html to DFD.png...")
    
    # Since we need to convert HTML to PNG, this requires special tools
    # First, let's check if we have the required libraries
    try:
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        
        # Setup Chrome options for headless browsing
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        
        # Create a temporary HTML file to ensure proper formatting
        driver = webdriver.Chrome(options=chrome_options)
        
        # Load the HTML file
        file_url = f"file://{os.path.abspath(input_file)}"
        driver.get(file_url)
        
        # Take a screenshot of the entire page
        driver.set_window_size(1200, 800)  # Set window size
        driver.save_screenshot(output_file)
        
        driver.quit()
        
        print(f"Successfully converted DFD.html to DFD.png")
        print(f"Output file: {output_file}")
        
    except ImportError:
        # If selenium is not available, try using playwright
        try:
            from playwright.sync_api import sync_playwright
            
            with sync_playwright() as p:
                browser = p.chromium.launch(headless=True)
                page = browser.new_page()
                
                # Load the HTML file
                file_path = os.path.abspath(input_file)
                page.goto(f"file://{file_path}")
                
                # Set viewport size
                page.set_viewport_size({"width": 1200, "height": 800})
                
                # Take screenshot
                page.screenshot(path=output_file, full_page=True)
                
                browser.close()
                
                print(f"Successfully converted DFD.html to DFD.png using Playwright")
                print(f"Output file: {output_file}")
                
        except ImportError:
            # If neither selenium nor playwright is available, inform the user
            print("Required libraries not available for HTML to PNG conversion.")
            print("To convert HTML to PNG, you need to install one of these packages:")
            print("  pip install selenium")
            print("  OR")
            print("  pip install playwright")
            print("  OR")
            print("  Use a web browser to manually export the HTML as PDF/PNG")
            return


if __name__ == "__main__":
    convert_html_to_png()