/**
 * Create placeholder images for existing items in ItemSystem
 */

const fs = require('fs');
const path = require('path');

// Create directories if they don't exist
const directories = [
    'assets/images/ships',
    'assets/images/weapons', 
    'assets/images/armors',
    'assets/images/items/materials',
    'assets/images/items/consumables',
    'assets/images/items/cosmetics',
    'assets/images/ui'
];

directories.forEach(dir => {
    const fullPath = path.join(__dirname, dir);
    if (!fs.existsSync(fullPath)) {
        fs.mkdirSync(fullPath, { recursive: true });
        console.log(`Created directory: ${dir}`);
    }
});

// Create a simple colored rectangle as placeholder
function createPlaceholder(width, height, color, text) {
    // Create a simple 1x1 pixel colored PNG (base64 encoded)
    // This is a minimal PNG with transparency
    const createColorPNG = (r, g, b) => {
        return Buffer.from([
            0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
            0x00, 0x00, 0x00, 0x0D, // IHDR chunk length
            0x49, 0x48, 0x44, 0x52, // IHDR
            width >> 24, width >> 16, width >> 8, width, // width
            height >> 24, height >> 16, height >> 8, height, // height
            0x08, 0x02, 0x00, 0x00, 0x00, // bit depth, color type, compression, filter, interlace
            0x4B, 0x6D, 0x29, 0xDC, // CRC
            0x00, 0x00, 0x00, 0x0C, // IDAT chunk length
            0x49, 0x44, 0x41, 0x54, // IDAT
            0x08, 0x99, 0x01, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, // compressed data
            0x00, 0x00, 0x00, 0x00, // IEND chunk length
            0x49, 0x45, 0x4E, 0x44, // IEND
            0xAE, 0x42, 0x60, 0x82  // CRC
        ]);
    };

    // For simplicity, let's copy existing images or create basic placeholders
    return createColorPNG(
        color === 'blue' ? 52 : color === 'red' ? 231 : color === 'green' ? 39 : 149,
        color === 'blue' ? 152 : color === 'red' ? 76 : color === 'green' ? 174 : 165,
        color === 'blue' ? 219 : color === 'red' ? 60 : color === 'green' ? 96 : 166
    );
}

// Items from your ItemSystem
const items = {
    ships: [
        'starter_cruiser_common',
        'starter_cruiser_uncommon', 
        'starter_cruiser_rare',
        'interceptor_common',
        'interceptor_uncommon'
    ],
    weapons: [
        'laser_pistol_common',
        'laser_pistol_uncommon',
        'laser_pistol_rare',
        'plasma_rifle_common',
        'plasma_rifle_uncommon',
        'plasma_rifle_rare'
    ],
    armors: [
        'light_armor_common',
        'light_armor_uncommon',
        'light_armor_rare',
        'medium_armor_common',
        'medium_armor_uncommon',
        'medium_armor_rare'
    ],
    materials: [
        'steel_plating',
        'energy_crystal',
        'rare_metal',
        'quantum_core',
        'nanomaterials',
        'dark_matter_fragment'
    ],
    consumables: [
        'health_pack',
        'energy_boost',
        'shield_recharge',
        'repair_kit',
        'ammo_pack',
        'experience_boost'
    ],
    cosmetics: [
        'cool_paint_job',
        'neon_lights',
        'custom_decal',
        'golden_trim',
        'carbon_fiber',
        'chrome_finish'
    ]
};

// Copy existing images or create placeholders
console.log('Creating placeholder images...');

// For ships - copy existing or create placeholder
items.ships.forEach(shipId => {
    const existingPath = path.join(__dirname, `assets/images/ships/starter_cruiser.png`);
    const targetPath = path.join(__dirname, `assets/images/ships/${shipId}.png`);
    
    if (fs.existsSync(existingPath) && !fs.existsSync(targetPath)) {
        fs.copyFileSync(existingPath, targetPath);
        console.log(`Copied ship: ${shipId}.png`);
    } else if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(80, 80, 'blue', shipId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created ship placeholder: ${shipId}.png`);
    }
});

// For weapons - copy existing or create placeholder
items.weapons.forEach(weaponId => {
    const existingPath = path.join(__dirname, `assets/images/weapons/starter_blaster.png`);
    const targetPath = path.join(__dirname, `assets/images/weapons/${weaponId}.png`);
    
    if (fs.existsSync(existingPath) && !fs.existsSync(targetPath)) {
        fs.copyFileSync(existingPath, targetPath);
        console.log(`Copied weapon: ${weaponId}.png`);
    } else if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(64, 64, 'red', weaponId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created weapon placeholder: ${weaponId}.png`);
    }
});

// For armors - create placeholders
items.armors.forEach(armorId => {
    const targetPath = path.join(__dirname, `assets/images/armors/${armorId}.png`);
    if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(64, 64, 'green', armorId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created armor placeholder: ${armorId}.png`);
    }
});

// For materials - create placeholders
items.materials.forEach(materialId => {
    const targetPath = path.join(__dirname, `assets/images/items/materials/${materialId}.png`);
    if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(48, 48, 'gray', materialId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created material placeholder: ${materialId}.png`);
    }
});

// For consumables - create placeholders
items.consumables.forEach(consumableId => {
    const targetPath = path.join(__dirname, `assets/images/items/consumables/${consumableId}.png`);
    if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(48, 48, 'purple', consumableId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created consumable placeholder: ${consumableId}.png`);
    }
});

// For cosmetics - create placeholders
items.cosmetics.forEach(cosmeticId => {
    const targetPath = path.join(__dirname, `assets/images/items/cosmetics/${cosmeticId}.png`);
    if (!fs.existsSync(targetPath)) {
        const placeholder = createPlaceholder(64, 64, 'gold', cosmeticId);
        fs.writeFileSync(targetPath, placeholder);
        console.log(`Created cosmetic placeholder: ${cosmeticId}.png`);
    }
});

// Create UI placeholder
const placeholderPath = path.join(__dirname, 'assets/images/ui/placeholder.png');
if (!fs.existsSync(placeholderPath)) {
    const placeholder = createPlaceholder(80, 80, 'gray', 'placeholder');
    fs.writeFileSync(placeholderPath, placeholder);
    console.log('Created UI placeholder.png');
}

console.log('\n🎉 Placeholder images created successfully!');
console.log('📦 Your shop should now display items with placeholder images!');
console.log('🔄 Replace these with actual images as you create them.');
