<?php 

    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;
    
    function pixe_settings_init(){
		// general settings
		$general = get_option('pixe_general', array());
		if( empty($general) ){
			update_option('pixe_general', array(
				'builder-post-types' => array('post', 'page'), 
				'builder-user-role' => array('administrator', 'editor') 
			), false);
		}

		// typo settings
        $typo = get_option('pixe_typo', '');
        if( empty($typo) ){
            $typo = array( 
                'preset' => array( 
                    'secondary' => array( 
                        'value' => array('font-size' => '17px', 'font-weight' => '600'), 
                        'style' => array('desktop' => '.pixe-body [data-typo-preset="secondary"], .pixe-body [data-typo-preset="secondary"] .pixe-text{font-size: 17px;font-weight: 600;}')
                    ), 
                    'primary' => array( 
                        'value' => array('font-size' => '20px', 'font-weight' => '600'), 
                        'style' => array('desktop' => '.pixe-body [data-typo-preset="primary"], .pixe-body [data-typo-preset="primary"] .pixe-text{font-size: 20px;font-weight: 600;}')
                    )
                )
            );
            update_option('pixe_typo', $typo, true);
        }
	}

    function pixe_init_data(){
        $version = floatval(get_option('pixe-initial-data', '0'));

        if( empty($version) ){

            // controls
            $controls = file_get_contents(PIXE_LOCAL_URL . '/admin/data/control-preset.json');
            $controls = json_decode($controls, true);
            update_option('pixe_controls', $controls, true);

            // template
            $templates = file_get_contents(PIXE_LOCAL_URL . '/admin/data/templates.json');
            $templates = json_decode($templates, true);
            pixe_copy_templates($templates);

            // header & footer
            pixe_import_default_header();
            pixe_import_default_footer();
            
            // set version
            $version = update_option('pixe-initial-data', '1.0.0');
        }

    }

    function pixe_import_default_header(){

        // default header
        $header = file_get_contents(PIXE_LOCAL_URL . '/admin/data/default-header.json');
        $header = json_decode($header, true);

        // import menu
        if( !empty($header['menus']) ){
            foreach($header['menus'] as $menu){
                pixe_import_menu($menu);
            }
        }

        // import additional templates
        if( !empty($header['templates']) ){
            foreach($header['templates'] as $template){
                pixe_import_template($template);
            }
        }

        // import main template
        $header_id = pixe_import_template($header);
        update_option('pixe_default_header', $header_id, false);
    }

    function pixe_import_default_footer(){

        // default header
        $footer = file_get_contents(PIXE_LOCAL_URL . '/admin/data/default-footer.json');
        $footer = json_decode($footer, true);

        // import menu
        if( !empty($footer['menus']) ){
            foreach($footer['menus'] as $menu){
                pixe_import_menu($menu);
            }
        }

        // import additional templates
        if( !empty($footer['templates']) ){
            foreach($footer['templates'] as $template){
                pixe_import_template($template);
            }
        }

        // import main template
        $footer_id = pixe_import_template($footer);
        update_option('pixe_default_footer', $footer_id, false);
    }