<?php
$root = dirname(__DIR__);
$themes = array(
    'industrial'=>array('Industrial', array(31,78,121), array(112,150,174)),
    'education'=>array('Education', array(37,99,235), array(125,171,255)),
    'school'=>array('School', array(22,101,52), array(110,190,130)),
    'construction'=>array('Construction', array(180,116,48), array(225,172,96)),
    'tech'=>array('Technology', array(79,70,229), array(130,160,255)),
    'legal'=>array('Legal', array(60,72,88), array(132,146,166)),
    'finance'=>array('Finance', array(15,118,110), array(82,170,160)),
    'logistics'=>array('Logistics', array(14,116,144), array(85,180,205)),
    'agriculture'=>array('Agriculture', array(64,128,64), array(140,190,110)),
    'tourism'=>array('Tourism', array(19,124,160), array(100,190,210)),
    'interior'=>array('Interior', array(145,92,62), array(210,155,120)),
    'sports'=>array('Sports', array(220,80,60), array(245,150,90)),
);
$channels = array('news'=>2, 'case'=>2, 'product'=>3);

function asset_dir($dir){
    if(!is_dir($dir)) mkdir($dir, 0777, true);
}

function asset_text($im, $size, $x, $y, $text, $color){
    imagestring($im, $size, $x, $y, $text, $color);
}

function asset_draw_jpg($file, $title, $subtitle, $w, $h, $c1, $c2){
    asset_dir(dirname($file));
    $im = imagecreatetruecolor($w, $h);
    for($y=0; $y<$h; $y++){
        $r = (int)($c1[0] + ($c2[0] - $c1[0]) * $y / $h);
        $g = (int)($c1[1] + ($c2[1] - $c1[1]) * $y / $h);
        $b = (int)($c1[2] + ($c2[2] - $c1[2]) * $y / $h);
        $line = imagecolorallocate($im, $r, $g, $b);
        imageline($im, 0, $y, $w, $y, $line);
    }
    $panel = imagecolorallocatealpha($im, 255, 255, 255, 72);
    $soft = imagecolorallocatealpha($im, 255, 255, 255, 96);
    $text = imagecolorallocate($im, 22, 32, 44);
    $white = imagecolorallocate($im, 255, 255, 255);
    imagefilledrectangle($im, 60, 60, $w - 60, $h - 60, $panel);
    for($i=0; $i<9; $i++){
        $x = 90 + $i * (int)(($w - 180) / 9);
        imagefilledrectangle($im, $x, 145 + ($i % 3) * 24, $x + 54, $h - 145, $soft);
        imagefilledellipse($im, $x + 27, $h - 108, 54, 54, $soft);
    }
    asset_text($im, 5, 92, 88, $subtitle, $text);
    asset_text($im, 5, 92, 122, $title, $text);
    asset_text($im, 3, 92, $h - 88, 'YanShuCMS Theme Asset', $white);
    imagejpeg($im, $file, 86);
    imagedestroy($im);
}

foreach($themes as $key=>$meta){
    list($name, $c1, $c2) = $meta;
    asset_draw_jpg($root . "/themes/$key/assets/images/banner/banner-$key.jpg", "$name Banner", 'Enterprise Website', 1440, 620, $c1, $c2);
    asset_draw_jpg($root . "/themes/$key/assets/images/company/company-$key.jpg", "$name About", 'Company Profile', 900, 520, $c1, $c2);
    asset_draw_jpg($root . "/assets/uploads/banner/banner-$key.jpg", "$name Banner", 'Enterprise Website', 1440, 620, $c1, $c2);
    asset_draw_jpg($root . "/assets/uploads/company/company-$key.jpg", "$name About", 'Company Profile', 900, 520, $c1, $c2);
    foreach($channels as $channel=>$count){
        if($key === 'school' && $channel === 'news') $count = 3;
        for($i=1; $i<=$count; $i++){
            asset_draw_jpg($root . "/themes/$key/assets/images/$channel/$key-$channel-$i.jpg", "$name $channel $i", ucfirst($channel) . ' Image', 900, 520, $c1, $c2);
            asset_draw_jpg($root . "/assets/uploads/$channel/$key-$channel-$i.jpg", "$name $channel $i", ucfirst($channel) . ' Image', 900, 520, $c1, $c2);
        }
    }
}

echo "JPG assets generated.\n";
