<?php

namespace Picqer\Barcode;

class BarcodeGeneratorJPG extends BarcodeGenerator
{

    /**
     * Return a JPG image representation of barcode (requires GD or Imagick library).
     *
     * @param string $code code to print
     * @param string $type type of barcode:
     * @param int $widthFactor Width of a single bar element in pixels.
     * @param int $totalHeight Height of a single bar element in pixels.
     * @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
     * @return string image data or false in case of error.
     * @public
     */
    public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0),$filepath,$detail)
    {
        $barcodeData = $this->getBarcodeData($code, $type);

        // calculate image size
        $width = ($barcodeData['maxWidth'] * $widthFactor);
        $height = $totalHeight;

        if (function_exists('imagecreate')) {
            // GD library
            $imagick = false;
            $jpg = imagecreate($width, $height);
            $colorBackground = imagecolorallocate($jpg, 255, 255, 255);
            imagecolortransparent($jpg, $colorBackground);
            $colorForeground = imagecolorallocate($jpg, $color[0], $color[1], $color[2]);
        } elseif (extension_loaded('imagick')) {
            $imagick = true;
            $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
            $jpg = new \Imagick();
            $jpg->newImage($width, $height, 'none', 'jpg');
            $imageMagickObject = new \imagickdraw();
            $imageMagickObject->setFillColor($colorForeground);
        } else {
            return false;
        }

        // print bars
        $positionHorizontal = 0;
        foreach ($barcodeData['bars'] as $bar) {
            $bw = round(($bar['width'] * $widthFactor), 3);
            $bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3);
            if ($bar['drawBar']) {
                $y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
                // draw a vertical bar
                if ($imagick && isset($imageMagickObject)) {
                    $imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh));
                } else {
                    imagefilledrectangle($jpg, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh),
                        $colorForeground);
                }
            }
            $positionHorizontal += $bw;
        }
        ob_start();
        if ($imagick && isset($imageMagickObject)) {
            $jpg->drawImage($imageMagickObject);
            echo $jpg;
        } else {
            imagejpeg($jpg,$filepath);
            //imagedestroy($jpg);
        }
        $image = ob_get_clean();
        //return $image;

        //border image
        $border=40;
        $width=ImageSx($jpg);
        $height=ImageSy($jpg);
        $img_adj_width=$width+(2*$border);
        $img_adj_height=$height+(2*$border)+280;
        $newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);
        $border_color = imagecolorallocate($newimage, 255, 255, 255);
        imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);

        $textcolor = imagecolorallocate($newimage, 0, 0, 0);
        $yline=190;
        $font = 'LEELAWUI.TTF';
        imagettftext($newimage, 18, 0, 160, 40, $textcolor, $font, 'บริษัท อีฮงมดแดงมอเตอร์ ลิส จำกัด');

        imagettftext($newimage, 18, 0, 140, $yline, $textcolor, $font, $detail['code']);
        $yline+=30;
        imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'รหัสอ้างอิง : '.$detail['รหัสอ้างอิง']);
        $yline+=30;
        imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'รหัสลูกค้า : '.$detail['รหัสลูกค้า']);
        $yline+=30;
        imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'ชื่อ : '.$detail['ชื่อ']);
        $yline+=30;
        if(mb_strlen($detail['สินค้า'])>48){
            for ($i=0; $i < mb_strlen ($detail['สินค้า']); $i+=48){
                if($i==0){
                    imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'สินค้า : '.mb_substr($detail['สินค้า'], $i,48));
                }else{
                    imagettftext($newimage, 18, 0, 50, $yline, $textcolor, $font, mb_substr($detail['สินค้า'], $i,48));
                }
                $yline+=30;
            }
        }else{
            imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'สินค้า : '.$detail['สินค้า']);
            $yline+=30;
        }
        imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'เลขที่สัญญา : '.$detail['เลขที่สัญญา']);
        $yline+=30;     
        imagettftext($newimage, 18, 0, 30, $yline, $textcolor, $font, 'ค่างวด : '.number_format($detail['ค่างวด'], 2, '.', ',').' บาท');
        $yline+=30;
        // imagestring($newimage, 5, 280, 180, $text, $textcolor);
        // imagestring($newimage, 5, 60, 210, 'สินค้า:'.$detail['สินค้า'], $textcolor);
        // imagestring($newimage, 5, 60, 240, 'ราคา:'.$detail['ราคา'], $textcolor);
        // imagestring($newimage, 5, 60, 270, 'ชำระแล้ว:'.$detail['ชำระแล้ว'], $textcolor);
        // imagestring($newimage, 5, 60, 300, 'ค่างวด:'.$detail['ค่างวด'], $textcolor);


        imageCopyResized($newimage,$jpg,$border,$border+20,0,0,$width,$height,$width,$height);
        // ImageJpeg($newimage,$filepath,100);
        ImageJpeg(imagerotate($newimage, 90, $border_color),$filepath,100); // change here to $add2 if a new image is to be created
        chmod("$filepath",0666); // change here to $add2 if a new image is to be created

        //create preview image
        $newwidth = 240;
        $newheight = 240;
        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $prebg = imagecolorallocate($thumb, 255, 255, 255);
        imagefilledrectangle($thumb,0,0,$newwidth,$newheight,$prebg);
        $yline=140;
        // Resize
        imagecopyresized($thumb, $jpg, 10, 10, 0, 0, $newwidth-20, $newheight-140, $width, $height);
        imagettftext($thumb, 12, 0, 5, $yline, $textcolor, $font, 'เลขที่สัญญา : '.$detail['เลขที่สัญญา']);
        $yline+=30;
        for ($i=0; $i < mb_strlen ($detail['สินค้า']); $i+=31){
            imagettftext($thumb, 12, 0, 5, $yline, $textcolor, $font, mb_substr($detail['สินค้า'], $i,31));
            $yline+=30;
        }

        // Output
        imagejpeg($thumb,substr_replace($filepath,"_pre",strlen($filepath)-4,0));
        imagedestroy($jpg);
        return $filepath;
    }
}