package com.pqs9000.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpSession;

import com.njcn.utils.PubUtils;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SvgPngConverter {

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(SvgPngConverter.class);

    /**
     *@Description: 将svg字符串转换为png
     *@Author:
     *@param svgCode svg代码
     *@param pngFilePath  保存的路径
     *@throws IOException io异常
     *@throws TranscoderException svg代码异常
     */
    public static String convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException{

        File file = new File (pngFilePath);
        if(file.exists()){
        	file.delete();
        }

        FileOutputStream outputStream = null;
        try {
            file.createNewFile ();
            outputStream = new FileOutputStream (file);
            convertToPng (svgCode, outputStream);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close ();
                } catch (IOException e) {
                    logger.error("svn代码发生异常，异常是"+e.getMessage());
                }
            }
        }
        return "success";
    }

    /**
     *@Description: 将svgCode转换成png文件，直接输出到流中
     *@param svgCode svg代码
     *@param outputStream 输出流
     *@throws TranscoderException 异常
     *@throws IOException io异常
     */
    public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{
        try {
            byte[] bytes = svgCode.getBytes ("UTF-8");
            Transcoder t = new PNGTranscoder ();
            TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes));
            TranscoderOutput output = new TranscoderOutput (outputStream);
            t.transcode (input, output);
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close ();
                } catch (IOException e) {
                    logger.error("读出流数据异常，异常是"+e.getMessage());
                }
            }
        }
    }

    /**
     *@Description: 读取图片流信息
     *@param session svg代码
     *@param imagePath 图片路径
     *@param targetName 目标名称
     *@param Typename 类型名称
     */
    public static String getImageBASE64Encoder(HttpSession session,String imagePath,String targetName,String Typename) {
        InputStream inStream = null;
        byte[] data = null;
        try {
            inStream = new FileInputStream(imagePath);
            data = new byte[inStream.available()];
            inStream.read(data);
        } catch (IOException e) {
            logger.error("读取图片流失败：" + e.getMessage());
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                   logger.error("关闭输入流异常，异常为："+e.toString());
                }
            }
        }
        if(targetName.equals("相电压有效值")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_VrmsX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_VrmsN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_VrmsE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Vrms%", data);
        	}
        }
        if(targetName.equals("三相电压不平衡度")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_VunbalX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_VunbalN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_VunbalE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Vunbal%", data);
        	}
        }
        if(targetName.equals("电压总谐波畸变率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_ThdVX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_ThdVN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_ThdVE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_ThdV%", data);
        	}
        }
        if(targetName.equals("电压总谐波畸变率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_ThdVX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_ThdVN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_ThdVE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_ThdV%", data);
        	}
        }
        if(targetName.equals("电流有效值")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_IrmsX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_IrmsN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_IrmsE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Irms%", data);
        	}
        }
        if(targetName.equals("电流总谐波畸变率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_ThdIX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_ThdIN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_ThdIE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_ThdI%", data);
        	}
        }
        if(targetName.equals("频率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_FreDevX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_FreDevN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_FreDevE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_FreDev%", data);
        	}
        }
        if(targetName.equals("谐波电压含有率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("SpecMap_HVX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("SpecMap_HVN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("SpecMap_HVE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("SpecMap_HV%", data);
        	}
        }
        if(targetName.equals("谐波电流幅值")){
        	if(Typename.equals("最大值")){
                session.setAttribute("SpecMap_HIX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("SpecMap_HIN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("SpecMap_HIE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("SpecMap_HI%", data);
        	}
        }
        if(targetName.equals("三相有功总功率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_PsumX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_PsumN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_PsumE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Psum%", data);
        	}
        }
        if(targetName.equals("三相无功总功率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_QsumX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_QsumN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_QsumE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Qsum%", data);
        	}
        }
        if(targetName.equals("三相视在总功率")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_SsumX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_SsumN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_SsumE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Ssum%", data);
        	}
        }
        if(targetName.equals("短时电压闪变")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_PstX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_PstN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_PstE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Pst%", data);
        	}
        }
        if(targetName.equals("长时电压闪变")){
        	if(Typename.equals("最大值")){
                session.setAttribute("Chart_PltX", data);
        	}else if(Typename.equals("最小值")){
                session.setAttribute("Chart_PltN", data);
        	}else if(Typename.equals("平均值")){
        		session.setAttribute("Chart_PltE", data);
        	}else if(Typename.equals("CP95")){
        		session.setAttribute("Chart_Plt%", data);
        	}
        }
        if(targetName.equals("基波电压")){
    		session.setAttribute("Basic_Vrms", data);
        }
        if(targetName.equals("基波电流")){
    		session.setAttribute("Basic_Irms", data);
        }
        for(int num=1;num<26;num++){
            if(targetName.equals(String.valueOf(num)+"次谐波电压")){
        		session.setAttribute("SpecMap_HV"+String.valueOf(num), data);
            }
            if(targetName.equals(String.valueOf(num)+"次谐波电流")){
        		session.setAttribute("SpecMap_HI"+String.valueOf(num), data);
            }
        }
        if(targetName.equals("谐波电压频谱")){
    		session.setAttribute("HarmI", data);
        }
        if(targetName.equals("谐波电流频谱")){
    		session.setAttribute("HarmV", data);
        }

        return "success";
    }

}