// Apps/DemoApp/UltraCanvasCDRExamples.cpp
// CDR vector graphics demo examples for UltraCanvas Framework
// Version: 1.0.1
// Last Modified: 2026-05-01
// Author: UltraCanvas Framework

#include "UltraCanvasDemo.h"
#include "UltraCanvasButton.h"
#include "UltraCanvasLabel.h"
#include "UltraCanvasContainer.h"
#include "UltraCanvasWindow.h"
#include "../Plugins/Vector/CDR/UltraCanvasCDRPlugin.h"
#include <iostream>
#include <memory>

namespace UltraCanvas {

// ===== CDR DEMO HANDLER =====
    class CDRDemoHandler {
    private:
        std::shared_ptr<UltraCanvasWindow> fullscreenWindow;
        std::string cdrFilePath;

    public:
        CDRDemoHandler(const std::string& filePath) : cdrFilePath(filePath) {}

        void OnCDRClick() {
            if (!fullscreenWindow) {
                CreateFullscreenWindow();
            }
        }

        void CreateFullscreenWindow() {
            int screenWidth = 1920;
            int screenHeight = 1080;

            WindowConfig config;
            config.title = "CDR Fullscreen Viewer";
            config.width = screenWidth;
            config.height = screenHeight;
            config.x = 0;
            config.y = 0;
            config.type = WindowType::Fullscreen;
            config.resizable = false;

            fullscreenWindow = CreateWindow(config);
            fullscreenWindow->SetBackgroundColor(Color(32, 32, 32, 255));

            // Create fullscreen CDR element
            auto fullscreenCDR = std::make_shared<UltraCanvasCDRElement>(
                    "FullscreenCDR", 0, 50, screenWidth, screenHeight - 100);
            fullscreenCDR->SetFitMode(CDRFitMode::FitPage);

            if (!cdrFilePath.empty()) {
                fullscreenCDR->LoadFromFile(cdrFilePath);
            }

            fullscreenWindow->AddChild(fullscreenCDR);

            // Navigation buttons
            auto btnPrev = std::make_shared<UltraCanvasButton>("BtnPrev", 10, 10, 80, 30);
            btnPrev->SetText("◀ Prev");
            btnPrev->SetColors(Color(60, 60, 65, 255));
            btnPrev->SetTextColors(Colors::White);
            btnPrev->onClick = [fullscreenCDR]() {
                if (fullscreenCDR->IsLoaded()) {
                    int current = fullscreenCDR->GetCurrentPage();
                    if (current > 0) {
                        fullscreenCDR->SetCurrentPage(current - 1);
                    }
                }
            };
            fullscreenWindow->AddChild(btnPrev);

            auto btnNext = std::make_shared<UltraCanvasButton>("BtnNext", 100, 10, 80, 30);
            btnNext->SetText("Next ▶");
            btnNext->SetColors(Color(60, 60, 65, 255));
            btnNext->SetTextColors(Colors::White);
            btnNext->onClick = [fullscreenCDR]() {
                if (fullscreenCDR->IsLoaded()) {
                    int current = fullscreenCDR->GetCurrentPage();
                    if (current < fullscreenCDR->GetPageCount() - 1) {
                        fullscreenCDR->SetCurrentPage(current + 1);
                    }
                }
            };
            fullscreenWindow->AddChild(btnNext);

            // Page info label
            auto pageLabel = std::make_shared<UltraCanvasLabel>("PageLabel", 200, 10, 150, 30);
            pageLabel->SetTextColor(Colors::White);
            if (fullscreenCDR->IsLoaded()) {
                pageLabel->SetText("Page 1/" + std::to_string(fullscreenCDR->GetPageCount()));
            }
            fullscreenWindow->AddChild(pageLabel);

            // Update page label on page change
            fullscreenCDR->onPageChanged = [pageLabel, fullscreenCDR](int page) {
                pageLabel->SetText("Page " + std::to_string(page + 1) + "/" +
                                   std::to_string(fullscreenCDR->GetPageCount()));
            };

            // Zoom buttons
            auto btnZoomOut = std::make_shared<UltraCanvasButton>("BtnZoomOut", 400, 10, 40, 30);
            btnZoomOut->SetText("−");
            btnZoomOut->SetColors(Color(60, 60, 65, 255));
            btnZoomOut->SetTextColors(Colors::White);
            btnZoomOut->onClick = [fullscreenCDR]() {
                fullscreenCDR->SetFitMode(CDRFitMode::FitNone);
                fullscreenCDR->SetZoom(fullscreenCDR->GetZoom() / 1.25f);
            };
            fullscreenWindow->AddChild(btnZoomOut);

            auto btnZoomIn = std::make_shared<UltraCanvasButton>("BtnZoomIn", 450, 10, 40, 30);
            btnZoomIn->SetText("+");
            btnZoomIn->SetColors(Color(60, 60, 65, 255));
            btnZoomIn->SetTextColors(Colors::White);
            btnZoomIn->onClick = [fullscreenCDR]() {
                fullscreenCDR->SetFitMode(CDRFitMode::FitNone);
                fullscreenCDR->SetZoom(fullscreenCDR->GetZoom() * 1.25f);
            };
            fullscreenWindow->AddChild(btnZoomIn);

            auto btnFitPage = std::make_shared<UltraCanvasButton>("BtnFit", 500, 10, 80, 30);
            btnFitPage->SetText("Fit Page");
            btnFitPage->SetColors(Color(60, 60, 65, 255));
            btnFitPage->SetTextColors(Colors::White);
            btnFitPage->onClick = [fullscreenCDR]() {
                fullscreenCDR->SetFitMode(CDRFitMode::FitPage);
            };
            fullscreenWindow->AddChild(btnFitPage);

            // Instructions label
            auto instructionLabel = std::make_shared<UltraCanvasLabel>(
                    "Instructions", screenWidth - 200, 10, 190, 30);
            instructionLabel->SetText("Press ESC to close");
            instructionLabel->SetTextColor(Color(200, 200, 200, 255));
            fullscreenWindow->AddChild(instructionLabel);

            // ESC key handler
            fullscreenWindow->SetEventCallback([this](const UCEvent& event) {
                if (event.type == UCEventType::KeyUp && event.virtualKey == UCKeys::Escape) {
                    if (fullscreenWindow) {
                        fullscreenWindow->Close();
                        fullscreenWindow.reset();
                    }
                    return true;
                }
                return false;
            });

            fullscreenWindow->Show();
        }
    };

// ===== CDR VECTOR EXAMPLES IMPLEMENTATION =====
    std::shared_ptr<UltraCanvasUIElement> UltraCanvasDemoApplication::CreateCDRVectorExamples() {
        auto container = std::make_shared<UltraCanvasContainer>("CDRExamples", 0, 0, 1000, 780);
        container->SetBackgroundColor(Color(245, 245, 245, 255));

        // Title
        auto title = std::make_shared<UltraCanvasLabel>("CDRTitle", 10, 10, 600, 30);
        title->SetText("CorelDRAW CDR Graphics Demo - Click to View Fullscreen");
        title->SetFontSize(16);
        title->SetAutoResize(true);
        title->SetFontWeight(FontWeight::Bold);
        container->AddChild(title);

        // Description
        auto description = std::make_shared<UltraCanvasLabel>("CDRDescription", 10, 45, 700, 40);
        description->SetText("Click on CDR images to open in fullscreen mode. Use navigation buttons for multi-page files.\nPress ESC to close fullscreen view. Supports CDR, CMX, CCX, CDT formats.");
        description->SetFontSize(12);
        description->SetTextColor(Color(80, 80, 80, 255));
        container->AddChild(description);

        // Status label for feedback
        auto statusLabel = std::make_shared<UltraCanvasLabel>("CDRStatus", 10, 700, 980, 60);
        statusLabel->SetText("Ready. Click on a CDR file to view.");
        statusLabel->SetFontSize(11);
        statusLabel->SetTextColor(Color(60, 60, 60, 255));
        statusLabel->SetBackgroundColor(Color(230, 230, 230, 255));
        container->AddChild(statusLabel);

        // ===== CDR FILE 1 =====
        auto cdrContainer1 = std::make_shared<UltraCanvasContainer>("CDRContainer1", 20, 100, 300, 280);
        cdrContainer1->SetBackgroundColor(Colors::White);
        cdrContainer1->SetBorders(2, Color(180, 180, 180, 255));

        auto cdrElement1 = std::make_shared<UltraCanvasCDRElement>("CDR1", 10, 10, 280, 220);
        cdrElement1->SetFitMode(CDRFitMode::FitPage);

        std::string cdrFile1 = NormalizePath(GetResourcesDir() + "media/cdr/demo.cdr");
        if (cdrElement1->LoadFromFile(cdrFile1)) {
            statusLabel->SetText("Loaded: " + cdrFile1 + " (" +
                                 std::to_string(cdrElement1->GetPageCount()) + " pages)");
        }

        auto cdrLabel1 = std::make_shared<UltraCanvasLabel>("CDRLabel1", 10, 240, 280, 30);
        cdrLabel1->SetText("demo.cdr");
        cdrLabel1->SetAlignment(TextAlignment::Center);
        cdrLabel1->SetFontSize(11);
        cdrContainer1->AddChild(cdrLabel1);

        auto demoHandler1 = std::make_shared<CDRDemoHandler>(cdrFile1);
        cdrElement1->SetEventCallback([demoHandler1, cdrContainer1, statusLabel, cdrFile1](const UCEvent& event) {
            switch (event.type) {
                case UCEventType::MouseUp:
                    demoHandler1->OnCDRClick();
                    statusLabel->SetText("Opened fullscreen: " + cdrFile1);
                    return true;
                case UCEventType::MouseEnter:
                    cdrContainer1->SetBordersColor(Color(0, 122, 204, 255));
                    return true;
                case UCEventType::MouseLeave:
                    cdrContainer1->SetBordersColor(Color(180, 180, 180, 255));
                    return true;
                default:
                    return false;
            }
        });

        cdrContainer1->AddChild(cdrElement1);
        container->AddChild(cdrContainer1);
/*
        // ===== CDR FILE 2 =====
        auto cdrContainer2 = std::make_shared<UltraCanvasContainer>("CDRContainer2", 340, 100, 300, 280);
        cdrContainer2->SetBackgroundColor(Colors::White);
        cdrContainer2->SetBorders(2, Color(180, 180, 180, 255));

        auto cdrElement2 = std::make_shared<UltraCanvasCDRElement>("CDR2", 10, 10, 280, 220);
        cdrElement2->SetFitMode(CDRFitMode::FitPage);

        std::string cdrFile2 = NormalizePath(GetResourcesDir() + "media/cdr/logo.cdr");
        cdrElement2->LoadFromFile(cdrFile2);

        auto cdrLabel2 = std::make_shared<UltraCanvasLabel>("CDRLabel2", 10, 240, 280, 30);
        cdrLabel2->SetText("logo.cdr");
        cdrLabel2->SetAlignment(TextAlignment::Center);
        cdrLabel2->SetFontSize(11);
        cdrContainer2->AddChild(cdrLabel2);

        auto demoHandler2 = std::make_shared<CDRDemoHandler>(cdrFile2);
        cdrElement2->SetEventCallback([demoHandler2, cdrContainer2, statusLabel, cdrFile2](const UCEvent& event) {
            switch (event.type) {
                case UCEventType::MouseUp:
                    demoHandler2->OnCDRClick();
                    statusLabel->SetText("Opened fullscreen: " + cdrFile2);
                    return true;
                case UCEventType::MouseEnter:
                    cdrContainer2->SetBordersColor(Color(0, 122, 204, 255));
                    return true;
                case UCEventType::MouseLeave:
                    cdrContainer2->SetBordersColor(Color(180, 180, 180, 255));
                    return true;
                default:
                    return false;
            }
        });

        cdrContainer2->AddChild(cdrElement2);
        container->AddChild(cdrContainer2);
*/
        // ===== CDR FILE 3 (CMX format) =====
//        auto cdrContainer3 = std::make_shared<UltraCanvasContainer>("CDRContainer3", 660, 100, 300, 280);
//        cdrContainer3->SetBackgroundColor(Colors::White);
//        cdrContainer3->SetBorders(2, Color(180, 180, 180, 255));
//
//        auto cdrElement3 = std::make_shared<UltraCanvasCDRElement>("CDR3", 10, 10, 280, 220);
//        cdrElement3->SetFitMode(CDRFitMode::FitPage);
//
//        std::string cdrFile3 = NormalizePath(GetResourcesDir() + "media/cdr/artwork.cmx");
//        cdrElement3->LoadFromFile(cdrFile3);
//
//        auto cdrLabel3 = std::make_shared<UltraCanvasLabel>("CDRLabel3", 10, 240, 280, 30);
//        cdrLabel3->SetText("artwork.cmx");
//        cdrLabel3->SetAlignment(TextAlignment::Center);
//        cdrLabel3->SetFontSize(11);
//        cdrContainer3->AddChild(cdrLabel3);
//
//        auto demoHandler3 = std::make_shared<CDRDemoHandler>(cdrFile3);
//        cdrElement3->SetEventCallback([demoHandler3, cdrContainer3, statusLabel, cdrFile3](const UCEvent& event) {
//            switch (event.type) {
//                case UCEventType::MouseUp:
//                    demoHandler3->OnCDRClick();
//                    statusLabel->SetText("Opened fullscreen: " + cdrFile3);
//                    return true;
//                case UCEventType::MouseEnter:
//                    cdrContainer3->SetBordersColor(Color(0, 122, 204, 255));
//                    return true;
//                case UCEventType::MouseLeave:
//                    cdrContainer3->SetBordersColor(Color(180, 180, 180, 255));
//                    return true;
//                default:
//                    return false;
//            }
//        });
//
//        cdrContainer3->AddChild(cdrElement3);
//        container->AddChild(cdrContainer3);

        // ===== SECOND ROW =====

        // ===== CDR FILE 4 =====
        auto cdrContainer4 = std::make_shared<UltraCanvasContainer>("CDRContainer4", 20, 400, 300, 280);
        cdrContainer4->SetBackgroundColor(Colors::White);
        cdrContainer4->SetBorders(2, Color(180, 180, 180, 255));

        auto cdrElement4 = std::make_shared<UltraCanvasCDRElement>("CDR4", 10, 10, 280, 220);
        cdrElement4->SetFitMode(CDRFitMode::FitPage);

        std::string cdrFile4 = NormalizePath(GetResourcesDir() + "media/cdr/logo.cdr");
        cdrElement4->LoadFromFile(cdrFile4);

        // Page navigation for multi-page document
        auto prevBtn4 = std::make_shared<UltraCanvasButton>("Prev4", 10, 240, 60, 25);
        prevBtn4->SetText("◀");
        prevBtn4->SetFontSize(10);
        prevBtn4->onClick = [cdrElement4]() {
            if (cdrElement4->IsLoaded() && cdrElement4->GetCurrentPage() > 0) {
                cdrElement4->SetCurrentPage(cdrElement4->GetCurrentPage() - 1);
            }
        };
        cdrContainer4->AddChild(prevBtn4);

        auto pageLabel4 = std::make_shared<UltraCanvasLabel>("PageLabel4", 80, 240, 140, 25);
        pageLabel4->SetText("brochure.cdr");
        pageLabel4->SetAlignment(TextAlignment::Center);
        pageLabel4->SetFontSize(10);
        cdrContainer4->AddChild(pageLabel4);

        cdrElement4->onPageChanged = [pageLabel4, cdrElement4](int page) {
            pageLabel4->SetText("Page " + std::to_string(page + 1) + "/" +
                                std::to_string(cdrElement4->GetPageCount()));
        };

        auto nextBtn4 = std::make_shared<UltraCanvasButton>("Next4", 230, 240, 60, 25);
        nextBtn4->SetText("▶");
        nextBtn4->SetFontSize(10);
        nextBtn4->onClick = [cdrElement4]() {
            if (cdrElement4->IsLoaded() &&
                cdrElement4->GetCurrentPage() < cdrElement4->GetPageCount() - 1) {
                cdrElement4->SetCurrentPage(cdrElement4->GetCurrentPage() + 1);
            }
        };
        cdrContainer4->AddChild(nextBtn4);

        auto demoHandler4 = std::make_shared<CDRDemoHandler>(cdrFile4);
        cdrElement4->SetEventCallback([demoHandler4, cdrContainer4, statusLabel, cdrFile4](const UCEvent& event) {
            switch (event.type) {
                case UCEventType::MouseUp:
                    demoHandler4->OnCDRClick();
                    statusLabel->SetText("Opened fullscreen: " + cdrFile4);
                    return true;
                case UCEventType::MouseEnter:
                    cdrContainer4->SetBordersColor(Color(0, 122, 204, 255));
                    return true;
                case UCEventType::MouseLeave:
                    cdrContainer4->SetBordersColor(Color(180, 180, 180, 255));
                    return true;
                default:
                    return false;
            }
        });

        cdrContainer4->AddChild(cdrElement4);
        container->AddChild(cdrContainer4);
/*
        // ===== ZOOM DEMO (CDR FILE 5) =====
        auto cdrContainer5 = std::make_shared<UltraCanvasContainer>("CDRContainer5", 340, 400, 300, 280);
        cdrContainer5->SetBackgroundColor(Colors::White);
        cdrContainer5->SetBorders(2, Color(180, 180, 180, 255));

        auto cdrElement5 = std::make_shared<UltraCanvasCDRElement>("CDR5", 10, 10, 280, 220);
        cdrElement5->SetFitMode(CDRFitMode::FitPage);

        std::string cdrFile5 = NormalizePath(GetResourcesDir() + "media/cdr/detailed.cdr");
        cdrElement5->LoadFromFile(cdrFile5);

        // Zoom controls
        auto zoomOutBtn5 = std::make_shared<UltraCanvasButton>("ZoomOut5", 10, 240, 50, 25);
        zoomOutBtn5->SetText("−");
        zoomOutBtn5->onClick = [cdrElement5]() {
            cdrElement5->SetFitMode(CDRFitMode::FitNone);
            cdrElement5->SetZoom(cdrElement5->GetZoom() / 1.25f);
        };
        cdrContainer5->AddChild(zoomOutBtn5);

        auto zoomLabel5 = std::make_shared<UltraCanvasLabel>("ZoomLabel5", 70, 240, 100, 25);
        zoomLabel5->SetText("Zoom Demo");
        zoomLabel5->SetAlignment(TextAlignment::Center);
        zoomLabel5->SetFontSize(10);
        cdrContainer5->AddChild(zoomLabel5);

        auto zoomInBtn5 = std::make_shared<UltraCanvasButton>("ZoomIn5", 180, 240, 50, 25);
        zoomInBtn5->SetText("+");
        zoomInBtn5->onClick = [cdrElement5]() {
            cdrElement5->SetFitMode(CDRFitMode::FitNone);
            cdrElement5->SetZoom(cdrElement5->GetZoom() * 1.25f);
        };
        cdrContainer5->AddChild(zoomInBtn5);

        auto fitBtn5 = std::make_shared<UltraCanvasButton>("Fit5", 240, 240, 50, 25);
        fitBtn5->SetText("Fit");
        fitBtn5->onClick = [cdrElement5]() {
            cdrElement5->SetFitMode(CDRFitMode::FitPage);
        };
        cdrContainer5->AddChild(fitBtn5);

        auto demoHandler5 = std::make_shared<CDRDemoHandler>(cdrFile5);
        cdrElement5->SetEventCallback([demoHandler5, cdrContainer5, statusLabel, cdrFile5](const UCEvent& event) {
            switch (event.type) {
                case UCEventType::MouseUp:
                    demoHandler5->OnCDRClick();
                    statusLabel->SetText("Opened fullscreen: " + cdrFile5);
                    return true;
                case UCEventType::MouseEnter:
                    cdrContainer5->SetBordersColor(Color(0, 122, 204, 255));
                    return true;
                case UCEventType::MouseLeave:
                    cdrContainer5->SetBordersColor(Color(180, 180, 180, 255));
                    return true;
                default:
                    return false;
            }
        });

        cdrContainer5->AddChild(cdrElement5);
        container->AddChild(cdrContainer5);
*/
        // ===== INFO PANEL =====
        auto infoContainer = std::make_shared<UltraCanvasContainer>("InfoPanel", 660, 400, 300, 280);
        infoContainer->SetBackgroundColor(Color(240, 248, 255, 255));
        infoContainer->SetBorders(2, Color(100, 149, 237, 255));

        auto infoTitle = std::make_shared<UltraCanvasLabel>("InfoTitle", 10, 10, 280, 25);
        infoTitle->SetText("CDR Plugin Features");
        infoTitle->SetFontWeight(FontWeight::Bold);
        infoTitle->SetFontSize(13);
        infoContainer->AddChild(infoTitle);

        auto infoText = std::make_shared<UltraCanvasLabel>("InfoText", 10,55, 280, 200);
        infoText->SetText(
                "✓ CorelDRAW CDR format\n"
                "✓ Corel Presentation Exchange CMX\n"
                "✓ Multi-page document support\n"
                "✓ Vector paths and shapes\n"
                "✓ Text with styling\n"
                "✓ Transformations (rotate, scale)\n"
                "✓ Groups and layers\n"
                "✓ Stroke and fill styles\n"
                "✓ Zoom and pan controls\n"
                "✓ Fit modes (page, width, height)\n"
                "\n"
                "Uses libcdr for parsing."
        );
        infoText->SetFontSize(11);
        infoText->SetTextColor(Color(50, 50, 50, 255));
        infoContainer->AddChild(infoText);

        container->AddChild(infoContainer);

        return container;
    }

} // namespace UltraCanvas