<?php

namespace HelpGent\App\Providers;

defined( 'ABSPATH' ) || exit;

use Exception;
use HelpGent\App\DTO\FormDTO;
use HelpGent\App\Models\Form;
use HelpGent\WpMVC\Contracts\Provider;

use HelpGent\App\BackgroundServices\FontBackgroundService;

use stdClass;

class FontFamilyServiceProvider implements Provider {
    public ?FontBackgroundService $background_service;

    public function boot() {
        add_action( 'init', [ $this, 'init_background_service' ] );
        add_action( 'helpgent_after_update_form', [$this, 'after_update_form'] );
        add_action( 'helpgent_after_delete_form', [$this, 'delete_font_family'] );
    }

    public function init_background_service(): void {
        $this->background_service = helpgent_singleton( FontBackgroundService::class );
    }

    public function after_update_form( FormDTO $form_dto ) {
        $form_content = json_decode( $form_dto->get_content(), true );

        if ( empty( $form_content['global']['font_family']['family'] ) ) {
            return;
        }

        $font_family = $form_content['global']['font_family']['family'];

        $this->download( $font_family );
    }

    public function delete_font_family( stdClass $form ) {
        /**
         * If the font family is empty then return or if the font is Inter then return too, Because inter is the plugin's primary font family
         */
        if ( empty( $form->font_family ) || "Inter" === $form->font_family ) {
            return;
        }

        /**
         * Checking if others forms are using the same font families
         */
        $other_forms = Form::query()->select( 1 )->where( 'font_family', $form->font_family )->first();

        if ( ! empty( $other_forms ) ) {
            return;
        }

        /**
         * Finally, Remove font family all variations
         */
        $directory = helpgent_font_family_dir( $form->font_family );

        $fonts = glob( $directory . '/*' );
        array_map( 'unlink', $fonts );

        helpgent_filesystem()->rmdir( $directory );
    }

    public function download( string $font_family ) {
        if ( ! $this->background_service ) {
            return;
        }

        $fonts = json_decode( file_get_contents( helpgent_dir( 'app/fonts.json' ) ), true );

        if ( empty( $fonts[ $font_family ]['files'] ) ) {
            return false;
        }

        $font_files = $fonts[ $font_family ]['files'];

        $directory = helpgent_font_family_dir();

        foreach ( $font_files as $weight => $file ) {
            $file_name = $directory . "{$font_family}/{$weight}.woff";

            if ( ! is_file( $file_name ) ) {
                $this->background_service->add_service_task(
                    'download_font',
                    [ 'font_family' => $font_family ] 
                );
                break;
            }
        }

        try {
            $this->background_service->run();
        } catch ( Exception $th ) {
        }
    }
}