<?php

class NCPC_Post_Meta_upgrader
{
    function __construct()
    {
    }
    function updateSizeOptions($sizes)
    {
        foreach ($sizes as &$size) { // Utilisation de référence (&)
            if (!isset($size["resizeSettings"])) {
                $size["resizeSettings"] = [
                    'autoResize' => true,
                    'desktop' => [
                        'defaultFontSize' => 110,
                        'maxFontSize' => 110,
                        'minFontSize' => 30
                    ],
                    'mobile' => [
                        'defaultFontSize' => 60,
                        'maxFontSize' => 60,
                        'minFontSize' => 30
                    ]
                ];
            }
        }
        return $sizes;
    }

    function updateFonts($fonts)
    {
        foreach ($fonts as &$font) { // Utilisation de référence (&)
            if (!isset($font['previewImg'])) {
                $font['previewImg'] = "";
            }
        }
        return $fonts;
    }

    function updateLetterTypes($letterTypes)
    {
        foreach ($letterTypes as &$letterType) { // Utilisation de référence (&)
            if (!isset($letterType["previewImg"])) {
                $letterType["previewImg"] = "";
            }
        }
        return $letterTypes;
    }

    function updateBackboards($backboards)
    {
        foreach ($backboards as &$backboard) { // Utilisation de référence (&)
            if (!isset($backboard["previewImg"])) {
                $backboard["previewImg"] = "";
            }
            if (!isset($backboard["backboardLength"])) {
                $backboard["backboardLength"] = [
                    'shapeLength' => 8,
                    'layers' => [
                        'activate' => false,
                        'options' => [
                            [
                                'label' => "",
                                'description' => "",
                                'length' => 8
                            ]
                        ]
                    ]
                ];
            }
            if ($backboard["type"] == "cut-to-shape") {
                $backboard["type"] = "cut-to-letter";
            }
        }
        return $backboards;
    }

    function updateBackboardsColors($colors)
    {
        $defaults = [
            'layers' => [],
            'pattern' => "",
            'usePattern' => false
        ];
        foreach ($colors as &$color) {
            foreach ($defaults as $defaultKey => $defaultValue) {
                if (!isset($color[$defaultKey])) {
                    $color[$defaultKey] = $defaultValue;
                }
            }
        }
        return $colors;
    }

    function updateMaterials($materials)
    {
        foreach ($materials as &$material) { // Utilisation de référence (&)
            if (!isset($material['previewImg'])) {
                $material['previewImg'] = "";
            }
        }
        return $materials;
    }

    function updateJackets($jackets)
    {
        foreach ($jackets as &$jacket) { // Utilisation de référence (&)
            if (!isset($jacket['previewImg'])) {
                $jacket['previewImg'] = "";
            }
        }
        return $jackets;
    }

    function updateMountings($mountings)
    {
        foreach ($mountings as &$mounting) { // Utilisation de référence (&)
            if (!isset($mounting['previewImg'])) {
                $mounting['previewImg'] = "";
            }
        }
        return $mountings;
    }

    function update_meta_structure($old_data, $new_data)
    {
        foreach ($new_data as $key => $value) {
            if (array_key_exists($key, $old_data)) {
                if (is_array($value) && is_array($old_data[$key])) {
                    $old_data[$key] = $this->update_meta_structure($old_data[$key], $value);
                }
            } else {
                $old_data[$key] = $value;
            }
        }
        return $old_data;
    }

    function updateRequiredOptions($requiredOptions, $isChannel = false)
    {
        $requiredOptions["sizeOptions"]["sizes"] = $this->updateSizeOptions($requiredOptions["sizeOptions"]["sizes"]);
        $requiredOptions["fontOptions"]["fonts"] = $this->updateFonts($requiredOptions["fontOptions"]["fonts"]);
        if ($isChannel) {
            $requiredOptions["letterTypesOptions"]["letterTypes"] = $this->updateLetterTypes($requiredOptions["letterTypesOptions"]["letterTypes"]);
        }
        return $requiredOptions;
    }

    function updateAdditionalOptions($additionalOptions)
    {
        $additionalOptions["backboardOptions"]["backboards"] = $this->updateBackboards($additionalOptions["backboardOptions"]["backboards"]);
        $additionalOptions["backboardColorsOptions"]["backboardColors"] = $this->updateBackboardsColors($additionalOptions["backboardColorsOptions"]["backboardColors"]);
        $additionalOptions["materialOptions"]["materials"] = $this->updateMaterials($additionalOptions["materialOptions"]["materials"]);
        $additionalOptions["mountingOptions"]["mountings"] = $this->updateMountings($additionalOptions["mountingOptions"]["mountings"]);
        $additionalOptions["jacketOptions"]["jackets"] = $this->updateJackets($additionalOptions["jacketOptions"]["jackets"]);
        return $additionalOptions;
    }

    function update_settings($oldSettings)
    {
        $newSettings = [
            'generals' => [
                'customizer' => [
                    'measurementUnit' => 'cm',
                    'showHideMeasurements' => 'both',
                    'decimalFormatMeasurements' => 'no-decimal',
                    'fontFamilyName' => '',
                    'priceStartOptions' => 'at-zero',
                    'priceMeasurementAnimation' => 'on',
                    'destokColumnOrder' => 'right',
                    'glowSwitch' => 'glow-only',
                    'discount' => 'none',
                    'displayOptions' => 'name',
                    'discountValue' => 0,
                    'showDayNightButton' => true,
                    'textAlignment' => 'display-alignment',
                ],
                'mobile' => [
                    'showNavigatorMenu' => 'off',
                    'showNavigationMenuFirst' => 'yes',
                    'mobileSelectionOptionsDisplay' => 'horizontally-stack',
                ],
                'output' => [
                    'fileFormat' => 'svg',
                    'manufacturerEmail' => [
                        'sendDesignByEmail' => true,
                        'receiverEmail' => [],
                        'subject' => 'New order for Manufacturer',
                        'emailTemplate' => '{{#product}}
        <p><br>
        <b>Text:</b> {{ncpc_text}}<br>
        <b>Alignment:</b> {{ncpc_text_align}}<br>
        <b>Font:</b> {{ncpc_font_family}}<br>
        <b>Size:</b> {{ncpc_size}}<br>
        <b>Colour:</b> {{ncpc_neon_color}}<br>
        <b>Backboard:</b> {{ncpc_backboard}}<br>
        <b>Backboard Colour:</b> {{ncpc_backboard_color}}<br>
        <b>Material:</b> {{ncpc_material}}<br>
        <b>Jacket:</b> {{ncpc_jacket}}<br>
        <b>Mounting:</b> {{ncpc_mounting}}<br>
        {{#additonal_options}}<b>{{label}}:</b> {{value}}<br>{{/additonal_options}}
         <img src="{{ncpc_preview_img}}"><br><br>
        {{#svgPreviewLink}}
        <b>SVG:</b> <a href="{{ncpc_svg_data}}" target="_blank">Link</a><br>
        {{/svgPreviewLink}}</p>
        {{/products}}<br><b>Name:</b> {{shipping_adddress.name}} <br>
        <b>Phone:</b> {{shipping_adddress.phone}} <br>
        <b>Address:</b> {{shipping_adddress.address1}} {{shipping_adddress.address2}} {{shipping_adddress.city}} {{shipping_adddress.country}} <br>
        <b>Shipping option:</b> {{shippingLine}} <br>
        <b>Email:</b> {{email}} <br>',
                    ],
                ],
                'product' => [
                    'enableAddToCart' => true,
                    'redirectAfterAddToCart' => true,
                    'redirectToCheckOutPage' => false,
                    'displayRecapsOnCheckout' => false,
                    'hideAddToCartButtonCustomProducts' => true,
                    'hideDesignButtonsOnShopPage' => false,
                    'hideAddToCartButtonOnShopPage' => true,
                ],
                'requestQuote' => [
                    "enableRequestQuote" => false,
                    "receiversEmail" => "",
                    "sendToCustomer" => false,
                    "acceptExtensions" => ['.jpg', '.png', '.svg', '.pdf'],
                    "maxFileSize" => 10,
                    "maxFilesNumber" => 5,
                    "emailSubject" => "Request A Quote"
                ],
                "mode" => [
                    "type" => "simple",
                    "allowMultiFonts" => true,
                    "allowMultiColors" => true,
                ]
            ],
            'languageImages' => [
                'customDesign' => [
                    'link' => [
                        'activate' => false,
                        'link' => '',
                        'phraseSubmitCustom' => 'Take a customization',
                    ],
                    'screen' => [
                        'activate' => false,
                        'customDesignText' => 'To submit the personalized design, please click on this button before',
                        'buttonLabel' => 'Send Custom Design',
                        'phraseToLinkBackTextCustomiser' => 'Custom now',
                        'steps' => [
                            'headers' => '',
                            'data' => [],
                        ],
                    ],
                ],
                'images' => [
                    'previewImage' => [
                        'activate' => false,
                        'allowUpload' => false,
                        'uploadText' => '',
                        'displayDefaultBackgroundImage' => false,
                    ],
                    'enableReviewImage' => true,
                    'reviewScreenImages' => [
                        'activate' => false,
                        'displayDefaultBackgroundImage' => false,
                    ],
                    'manageImages' => [
                        'ncpcData.ncpc_assets_url/images/include-imgs/img13.jpg',
                        'ncpcData.ncpc_assets_url/images/include-imgs/img14.jpg',
                        'ncpcData.ncpc_assets_url/images/include-imgs/img15.jpg',
                        'ncpcData.ncpc_assets_url/images/include-imgs/img21.jpg',
                        'ncpcData.ncpc_assets_url/images/include-imgs/img24.jpg',
                        'ncpcData.ncpc_assets_url/images/include-imgs/img27.jpg',
                    ],
                ],
                'main' => [
                    'header' => 'CREATE YOUR SIGN',
                    'textBoxLabel' => 'Write your text',
                    'lineCount' => 'Lines',
                    'maxCharacters' => 'max characters per line for current size',
                    'addingToCartButton' => 'Add to the cart',
                    'requestAQuoteButton' => 'Get A Quote',
                    "buttonSendAQuoteFormLabel" => "Submit",
                    "firstName" => "First Name",
                    "lastName" => "Last Name",
                    "email" => "Email",
                    "phone" => "Phone",
                    "quoteAdditionalNotes" => "Additional Notes",
                    "quoteUploadFileTitle" => "Upload your files",
                    "quoteFilesFormats" => "Formats",
                    "maxFileSize" => "Max File Size",
                    "emailError" => "Please enter a valid email address",
                    "quoteThanksMessage" => "Your quote has been sent successfully",
                    "quoteModalTitle" => "Quote Request Submission",
                    'finishButton' => 'Finish',
                    'textBeforePriceValue' => '',
                    'textAfterPriceValue' => 'Tax included',
                    'maxSignLabel' => 'the size exceeds the limit',
                    'textAlignment' => 'Text Align',
                    'textAlignmentValue' => [
                        'left' => 'Left',
                        'center' => 'Center',
                        'right' => 'Right',
                    ],
                    'maxSignModals' => [
                        'width' => 'Your sign has exceeded the maximum width',
                        'height' => 'Your sign has exceeded the maximum height',
                    ],
                    'measurementLabel' => [
                        'width' => 'Width',
                        'height' => 'Height',
                    ],
                    'summaryLabel' => 'Summary',
                    'editLabel' => 'Edit',
                    'sectionText' => 'Text',
                    'sectionAdditionalOptions' => 'Additional Options',
                    'charactersRemaining' => 'Remaining characters',
                    'minimumCharacter' => 'or more characters are required, please add more characters.',
                ],
                'reviewScreen' => [
                    'activate' => true,
                    'mainReviewSection' => [
                        'headerLabel' => '',
                        'backLabel' => '',
                        'textFieldsDisplayUndeneathImageCarousel' => ['', '', ''],
                    ],
                ],
                'visualizer' => [
                    'defaultText' => 'Your Text',
                    'textUnderneathPrice' => '',
                    'showNote' => true,
                    'note' => '',
                ],
            ],
            'infos' => [
                'sizes' => '',
                'backboards' => '',
                'mountings' => '',
                'customAdditionalsOptions' => [],
            ],
            'themes' => [
                'skin' => 'default',
                'colors' => [
                    'backgroundColor' => '#FFFFFF',
                ],
            ],
            'sortOptions' => [],
        ];
        return $this->update_meta_structure($oldSettings, $newSettings);
    }
}
