#target illustrator

function main() {
    // ScriptUI Dialog
    var dlg = new Window('dialog', 'Batch Icon Board Generator');
    dlg.alignChildren = 'fill';

    // Title & Subtitle
    var titleGroup = dlg.add('panel', undefined, 'Title & Subtitle');
    titleGroup.orientation = 'column';
    titleGroup.alignChildren = 'left';
    titleGroup.margins = 10;
    titleGroup.add('statictext', undefined, 'Main Title:');
    var titleInput = titleGroup.add('edittext', undefined, 'TOOTH\nBRUSHING');
    titleInput.characters = 25;
    titleGroup.add('statictext', undefined, 'Subtitle:');
    var subtitleInput = titleGroup.add('edittext', undefined, '50 ICONS');
    subtitleInput.characters = 25;
    titleGroup.add('statictext', undefined, 'Title Font Size:');
    var fontSizeInput = titleGroup.add('edittext', undefined, '60');
    fontSizeInput.characters = 5;
    titleGroup.add('statictext', undefined, 'Subtitle Font Size:');
    var subFontSizeInput = titleGroup.add('edittext', undefined, '48');
    subFontSizeInput.characters = 5;

    // Color Picker Panel
    var colorGroup = dlg.add('panel', undefined, 'Block Color');
    colorGroup.orientation = 'row';
    colorGroup.alignChildren = 'left';
    colorGroup.margins = 10;
    var colorSample = colorGroup.add('panel', undefined, '');
    colorSample.preferredSize = [40, 20];
    colorSample.graphics.backgroundColor = colorSample.graphics.newBrush(colorSample.graphics.BrushType.SOLID_COLOR, [80/255,200/255,210/255,1]);
    var pickColorBtn = colorGroup.add('button', undefined, 'Pick Color');
    var pickedColor = [80,200,210]; // default

    pickColorBtn.onClick = function() {
        var c = $.colorPicker();
        if (c !== -1) {
            pickedColor = [
                (c >> 16) & 0xFF,
                (c >> 8) & 0xFF,
                c & 0xFF
            ];
            colorSample.graphics.backgroundColor = colorSample.graphics.newBrush(colorSample.graphics.BrushType.SOLID_COLOR, [pickedColor[0]/255, pickedColor[1]/255, pickedColor[2]/255, 1]);
        }
    }

    // Folder selection
    var folderGroup = dlg.add('panel', undefined, 'Folders');
    folderGroup.orientation = 'column';
    folderGroup.alignChildren = 'left';
    folderGroup.margins = 10;
    var inputFolderBtn = folderGroup.add('button', undefined, 'Select Input Folder');
    var inputFolderText = folderGroup.add('statictext', undefined, 'No folder selected', {truncate:'middle'});
    var outputFolderBtn = folderGroup.add('button', undefined, 'Select Output Folder');
    var outputFolderText = folderGroup.add('statictext', undefined, 'No folder selected', {truncate:'middle'});
    var inputFolder = null, outputFolder = null;

    inputFolderBtn.onClick = function() {
        inputFolder = Folder.selectDialog("Select folder with EPS files");
        if (inputFolder) inputFolderText.text = inputFolder.fsName;
    }
    outputFolderBtn.onClick = function() {
        outputFolder = Folder.selectDialog("Select output folder");
        if (outputFolder) outputFolderText.text = outputFolder.fsName;
    }

    // Buttons
    var btnGroup = dlg.add('group');
    btnGroup.alignment = 'center';
    var okBtn = btnGroup.add('button', undefined, 'Start Processing', {name:'ok'});
    var cancelBtn = btnGroup.add('button', undefined, 'Cancel', {name:'cancel'});

    // Show dialog
    if (dlg.show() != 1) return;

    // Validate
    if (!inputFolder || !outputFolder) {
        alert("Please select both input and output folders.");
        return;
    }

    // Get values
    var titleTextInput = titleInput.text;
    var subtitleTextInput = subtitleInput.text;
    // নিচের দুইটা ইনপুট আর ব্যবহার হবে না, কারণ আমরা ফন্ট সাইজ ম্যানুয়ালি সেট করবো
    // var fontSize = parseInt(fontSizeInput.text);
    // var subFontSize = parseInt(subFontSizeInput.text);
    var blockColor = makeRGB(pickedColor[0], pickedColor[1], pickedColor[2]);

    // EPS files
    var epsFiles = inputFolder.getFiles("*.eps");
    if (epsFiles.length === 0) {
        alert("No EPS files found in the folder.");
        return;
    }

    var batchNumber = 1;
    var totalFiles = epsFiles.length;

    for (var i = 0; i < totalFiles; i += 50) {
        // Artboard size (wide)
        var artboardWidth = 2000;
        var artboardHeight = 800;
        var doc = app.documents.add(DocumentColorSpace.RGB, artboardWidth, artboardHeight);

        // Block sizes (নমুনা অনুযায়ী)
        var blockWidth = 350;
        var blockHeight1 = 500; // উপরের বড় ব্লক
        var blockHeight2 = 270; // নিচের ছোট ব্লক
        var blockGap = 30;      // ব্লকের মাঝে ফাঁকা

        // --- উপরের বড় ব্লক ---
        var block1_top = artboardHeight; // artboard এর উপরের দিক
        var block1_left = 0;
        var colorBlock1 = doc.pathItems.rectangle(
            block1_top, block1_left, blockWidth, blockHeight1
        );
        colorBlock1.fillColor = blockColor;
        colorBlock1.stroked = false;

        // --- নিচের ছোট ব্লক ---
        var block2_top = artboardHeight - blockHeight1 - blockGap;
        var block2_left = 0;
        var colorBlock2 = doc.pathItems.rectangle(
            block2_top, block2_left, blockWidth, blockHeight2
        );
        colorBlock2.fillColor = blockColor;
        colorBlock2.stroked = false;

        // ----------- লেখার অংশ (ম্যানুয়ালি ছোট ও সুন্দর) -----------
        // --- উপরের ব্লকের মধ্যে লেখা (Main Title) ---
        var titleFontSize = 25; // ছোট ফন্ট সাইজ
        var titleMarginX = 50;  // ব্লকের দুই পাশে মার্জিন
        var titleMarginY = 50;  // ব্লকের উপর-নিচে মার্জিন
        var titleText = doc.textFrames.add();
        titleText.contents = titleTextInput.toUpperCase();
        titleText.textRange.characterAttributes.size = titleFontSize;
        titleText.textRange.characterAttributes.fillColor = makeWhite();
        titleText.textRange.justification = Justification.CENTER;
        titleText.width = blockWidth - titleMarginX * 2;
        titleText.height = blockHeight1 - titleMarginY * 2;
        titleText.left = block1_left + titleMarginX;
        titleText.top = block1_top - titleMarginY;
        try {
            titleText.textRange.characterAttributes.textFont = app.textFonts.getByName("Arial-BoldMT");
        } catch(e) {}

        // --- নিচের ব্লকের মধ্যে লেখা (Subtitle) ---
        var subFontSize = 25; // ছোট ফন্ট সাইজ
        var subMarginX = 50;
        var subMarginY = 50;
        var subtitleText = doc.textFrames.add();
        subtitleText.contents = subtitleTextInput.toUpperCase();
        subtitleText.textRange.characterAttributes.size = subFontSize;
        subtitleText.textRange.characterAttributes.fillColor = makeWhite();
        subtitleText.textRange.justification = Justification.CENTER;
        subtitleText.width = blockWidth - subMarginX * 2;
        subtitleText.height = blockHeight2 - subMarginY * 2;
        subtitleText.left = block2_left + subMarginX;
        subtitleText.top = block2_top - subMarginY;
        try {
            subtitleText.textRange.characterAttributes.textFont = app.textFonts.getByName("Arial-BoldMT");
        } catch(e) {}
        // ----------- লেখার অংশ শেষ -----------

        // Grid settings (right side)
        var gridLeft = blockWidth + 30;
        var gridTop = artboardHeight - 30;
        var gridWidth = artboardWidth - gridLeft - 30;
        var gridHeight = artboardHeight - 60;
        var rows = 5, cols = 10;
        var cellWidth = gridWidth / cols;
        var cellHeight = gridHeight / rows;

        // Place icons
        for (var j = 0; j < 50; j++) {
            var fileIndex = i + j;
            if (fileIndex >= totalFiles) break;

            var row = Math.floor(j / cols);
            var col = j % cols;
            var xPos = gridLeft + col * cellWidth;
            var yPos = gridTop - row * cellHeight;

            var epsFile = epsFiles[fileIndex];
            var placedItem = doc.placedItems.add();
            placedItem.file = epsFile;

            // Maintain aspect ratio and scale to fit cell (90%)
            var scaleFactor = Math.min(
                (cellWidth * 0.9) / placedItem.width,
                (cellHeight * 0.9) / placedItem.height
            );
            placedItem.width *= scaleFactor;
            placedItem.height *= scaleFactor;
            placedItem.left = xPos + (cellWidth - placedItem.width) / 2;
            placedItem.top = yPos - (cellHeight - placedItem.height) / 2;
            placedItem.embed();
        }

        // Add white background (optional)
        var backgroundLayer = doc.layers.add();
        backgroundLayer.name = "Background";
        var rect = backgroundLayer.pathItems.rectangle(
            artboardHeight, 0, artboardWidth, artboardHeight
        );
        rect.fillColor = makeWhite();
        rect.stroked = false;
        backgroundLayer.zOrder(ZOrderMethod.SENDTOBACK);

        // Save EPS
        var outputEPS = new File(outputFolder.fsName + "/Batch_" + batchNumber + ".eps");
        var epsOptions = new EPSSaveOptions();
        epsOptions.compatibility = Compatibility.ILLUSTRATOR10;
        doc.saveAs(outputEPS, epsOptions);

        // Export JPG
        var outputJPG = new File(outputFolder.fsName + "/Batch_" + batchNumber + ".jpg");
        var jpgOptions = new ExportOptionsJPEG();
        jpgOptions.qualitySetting = 100;
        jpgOptions.horizontalScale = 100;
        jpgOptions.verticalScale = 100;
        jpgOptions.artBoardClipping = true;
        doc.exportFile(outputJPG, ExportType.JPEG, jpgOptions);

        doc.close(SaveOptions.DONOTSAVECHANGES);
        batchNumber++;
    }

    alert("All batches processed successfully!");
}

// Helper function for white color
function makeWhite() {
    var white = new RGBColor();
    white.red = 255;
    white.green = 255;
    white.blue = 255;
    return white;
}

// Helper: RGBColor
function makeRGB(r, g, b) {
    var rgb = new RGBColor();
    rgb.red = r;
    rgb.green = g;
    rgb.blue = b;
    return rgb;
}

main();
এই কোডটি তুমি ভালো করে পর্যবেক্ষণ করো এবং আমাকে বলে যে লেখার জন্য যে কোডটি ব্যবহার করা হয়েছে সেটি পরিবর্তন করা যাবে কি না।