package org.ts.ddcs.Worker;


import org.ts.ddcs.Entity.RsvrEntity;
import org.ts.ddcs.Entity.RtuBaseInfo;
import org.ts.ddcs.Entity.RtuImageEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Service.SwService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.Callable;


public class WaterLevelImageCompoundProcess implements Callable<Integer> {

  //  private FTPClient ftpClient = new FTPClient();
    private static String encoding = System.getProperty("file.encoding");


    @Override
    public Integer call() throws Exception {
//        try {
//            String localPath = DataProcessHolder.newInstance().dataProcess.config.getTmpPhotoPath();
//            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy_MM_dd");
//            SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyyMMddHH");
//            SimpleDateFormat timeFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//            ftpClient.setControlEncoding(encoding);
//            ftpClient.connect("172.1.0.150", 21);
//            ftpClient.login("picture", "n(Fgv8zX");// 登录
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            int reply = ftpClient.getReplyCode();
//            // 验证是否登陆成功
//            if (!FTPReply.isPositiveCompletion(reply)) {
//                ftpClient.disconnect();
//                System.err.println("FTP server refused connection.");
//                return 0;
//            }
//            List<RtuBaseInfo> stations = DataProcessHolder.newInstance().dataProcess.rtuService.cameraStation();
//            if (null != stations && stations.size() > 0) {
//                for (RtuBaseInfo rtuBaseInfo : stations) {
//                    RsvrEntity entity = DataProcessHolder.newInstance().dataProcess.swService.lastRsvrData(rtuBaseInfo.getRtuCode());
//                    if (null != entity) {
//                        Date tm = new Date(entity.getTM().getTime());
//                       // String groupId = entity.getSTCD() + timeFormat2.format(new Date(entity.getTM().getTime()));
//                        RtuImageEntity rtuImageEntity = DataProcessHolder.newInstance().dataProcess.swService.imageInfo(entity.getSTCD(),tm);
//                        if (null == rtuImageEntity) {
//                            String dir = "/RTU_PICTURE/" + timeFormat.format(tm) + "-" + timeFormat.format(tm);
//                            ftpClient.changeWorkingDirectory(new String(dir.getBytes(encoding), "iso-8859-1"));
//                            FTPFile[] fs = ftpClient.listFiles();
//                            if (null != fs && fs.length > 0) {
//                                for (FTPFile ff : fs) {
//                                    if (ff.isFile()) {
//                                        String fileName = ff.getName();
//                                        String[] names = fileName.split("_");
//                                        if (names.length >= 3) {
//                                            String rtuCode = names[0];
//                                            // String camId = names[1];
//                                            String time = names[2];
//                                            if (time.length() > 14) {
//                                                time = time.substring(0, 14);
//                                            }
//                                            if (rtuCode.equals(entity.getSTCD())) {
//                                                if (time.startsWith(timeFormat2.format(tm))) {
//                                                    File localFile = new File(localPath + "/" + ff.getName());
//                                                    OutputStream is = new FileOutputStream(localFile);
//                                                    ftpClient.retrieveFile(ff.getName(), is);
//                                                    is.close();
//                                                    Font font = new Font("微软雅黑", Font.PLAIN, 30);
//                                                    BufferedImage pic = ImageIO.read(localFile);
//                                                    Graphics2D graphics = (Graphics2D) pic.getGraphics();
//                                                    graphics.setFont(font);
//                                                    graphics.setColor(Color.gray);
//                                                    graphics.fillRect(pic.getWidth() - 650, 5, 650, 50);
//                                                    graphics.setColor(Color.yellow);
//                                                    String text = "采集时间: " + timeFormat3.format(new Date(entity.getTM().getTime()));
//                                                    text += " 水位: " + String.format("%.02f", entity.getRZ());
//                                                    graphics.drawString(text, pic.getWidth() - 640, 40);
//                                                    File toFile = new File(localPath + "/to-" + ff.getName());
//                                                    ImageIO.write(pic, "jpg", toFile);
//                                                    FileInputStream fileInputStream = new FileInputStream(toFile);
//                                                    FastImageFile fastImageFile = new FastImageFile(fileInputStream, toFile.length(), "jpg", null);
//                                                    StorePath storePath = DataProcessHolder.newInstance().dataProcess.fastFileStorageClient.uploadImage(fastImageFile);
//                                                    String photoUrlPath = storePath.getFullPath();
//                                                    SwService swService = DataProcessHolder.newInstance().dataProcess.swService;
//                                                    String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//                                                    RtuImageEntity imageEntity = new RtuImageEntity();
//                                                    //imageEntity.setGroupId(rtuCode + timeFormat2.format(new Date(entity.getTM().getTime())));
//                                                    imageEntity.setGroupId(rtuCode);
//                                                    imageEntity.setRtuCode(rtuCode);
//                                                    imageEntity.setFileName(fileName);
//                                                    imageEntity.setTm(entity.getTM());
//                                                    swService.SaveSwImage(uuid, photoUrlPath, imageEntity);
//                                                    break;
//                                                }
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
        return 0;
    }
}
