package cn.com.ctop.common.module.utils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.UUID;
import java.util.List;

public class ImageUtils {
    public BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see e661 Determining If an Image Has Transparent Pixels
        //boolean hasAlpha = hasAlpha(image);

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
       /* if (hasAlpha) {
         transparency = Transparency.BITMASK;
         }*/

            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                    image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            // The system does not have a screen
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
        /*if (hasAlpha) {
         type = BufferedImage.TYPE_INT_ARGB;
         }*/
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }

    public void resize(int width, int height, String sourcePath, String targetPath) {
        File inputFile = new File(sourcePath);
        File outputFile = new File(targetPath);
        try {
            Image sourceImage = ImageIO.read(inputFile);
            int sourceWidth = sourceImage.getWidth(null);
            int sourceHeight = sourceImage.getHeight(null);
            int newWidth = 0;
            int newHeight = 0;
            if (sourceWidth / sourceHeight >= width / height) {
                //放大高度
                newHeight = height;
                newWidth = (int) (((float) height / (float) sourceHeight) * sourceWidth);
            } else {
                //放大宽度
                newWidth = width;
                newHeight = (int) (((float) width / (float) sourceWidth) * sourceHeight);
            }
            Image resizeImage = sourceImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
            BufferedImage bufferedImage = toBufferedImage(resizeImage);
            bufferedImage = bufferedImage.getSubimage((newWidth - width) / 2, (newHeight - height) / 2, width, height);
            ImageIO.write(bufferedImage, "jpg", outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void addTextInImage(String theme, InputStream input, String content, Color color, Font font, String borderPath, String coverPath, int borderOffsiteX, int borderOffsiteY, int contentOffsiteX, int contentOffsiteY) throws IOException {
        Image image = ImageIO.read(input);
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.drawImage(image, 0, 0, width, height, null);
        graphics2D.setColor(color);
        graphics2D.setFont(font);
        File borderFile = new File(borderPath);

        Image borderImage = ImageIO.read(borderFile);
//        BufferedImage bufferedImage2 = new BufferedImage(borderImage.getWidth(null),borderImage.getHeight(null),BufferedImage.TYPE_INT_RGB);
        graphics2D.drawImage(borderImage, borderOffsiteX, height / 2 + borderOffsiteY, null);

//        graphics2D.setColor(Color.BLUE.darker().darker());
//        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        graphics2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
//        graphics2D.setStroke(new BasicStroke(5.0f ,
//                BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15.0f, new float[] {10.0f,10.0f}, 5.0f));
        FontRenderContext frc = graphics2D.getFontRenderContext();
        int x = contentOffsiteX;
        int y = height / 2 + contentOffsiteY;
//        if (content.length() < 9) {
//            graphics2D.drawString(content, x, y);
//        } else {
//            graphics2D.drawString(content.substring(0, 8), x, y);
//            graphics2D.drawString(content.substring(8), x, y + 60);
//        }
        AffineTransform affineTransform = new AffineTransform();
        affineTransform.rotate(Math.toRadians(-5), 360, 800);
//        Font rotatedFont = font.deriveFont(affineTransform);
//        graphics2D.setFont(font);
        graphics2D.setTransform(affineTransform);
        graphics2D.drawString(content, x, y);

//        graphics2D.setTransform(trans);
        graphics2D.dispose();
        FileOutputStream fileOutputStream = new FileOutputStream(coverPath + "/" + theme + "_" + font.getFontName() + "_" + borderFile.getName() + "_" + content + UUID.randomUUID() + ".jpg");
        ImageIO.write(bufferedImage, "jpg", fileOutputStream);

    }

    public int getContentLength(String content, Graphics2D graphics2D) {
        return graphics2D.getFontMetrics(graphics2D.getFont()).charsWidth(content.toCharArray(), 0, content.length());
    }
    public List<String> getContentList() {
        List<String> list = new ArrayList<String>();
        //秋装
//        list.add("不露腿也能穿得心机");
//        list.add("原来还能这么穿");
//        list.add("女生本来就该这样穿");
//        list.add("逆袭打脸渣男");
//        list.add("你适合穿裙子还是裤子");
//        list.add("同样的衣服，她却");
//        list.add("出门逛街的首选");
//        list.add("约会必备的穿搭");
//        list.add("这样穿直男瞬间KO");
//        list.add("女生还是这样穿漂亮");

        //显瘦
//        list.add("胖妹子救星一招显瘦");
//        list.add("微胖也能穿出女神范儿");
//        list.add("我不减肥都比你好看");
//        list.add("她再也不敢炫耀了");
//        list.add("显瘦本来就该这样穿");
//        list.add("变装后的差别待遇");
//        list.add("女孩变装后漂亮大方");
//        list.add("男人都喜欢这样的");
//        list.add("男友喜欢女生的样子");
//        list.add("发了工资一定要");

        //两件套
        list.add("这样的两件套真的值了");
        list.add("论两件套的正确穿搭");
        list.add("怎样穿更少女");
        list.add("减龄10岁的正确穿搭");
        list.add("这样穿就对了");
        list.add("女神衣柜必备套装");
        list.add("改变自己学习穿搭");
        list.add("这样穿不怕撞衫");
        list.add("见男友这样穿让他后悔");
        list.add("这样穿不简单");


        return list;
    }
}

