package org.ts.ddcs.DataController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataController.service.RtuDataService;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Worker.DataProcessHolder;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.conn.FdfsConnectionManager;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.GenerateStorageClient;
import com.sun.javafx.iio.ImageMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@SuppressWarnings("all")
public class DataController {

    @Autowired
    private RtuDataService rtuDataService;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    @GetMapping(value = "DataCollectServer/fdfs/test")
    @ResponseBody
    public JSONObject testfdfs() {
        JSONObject response = new JSONObject();
        try {

            String path = DataProcessHolder.newInstance().dataProcess.config.getRtuUploadPhotoPath() + File.separator + DataProcessHolder.newInstance().dataProcess.config.getImageServerPath();
            path += File.separator + "1.png";

            File file = new File(path);
            InputStream inputStream = new FileInputStream(file);
            //   IImageMetadata metadata = Sanselan.getMetadata(file);
            //    ImageMetadata metaData = new ImageMetadata();
            // metaData.();

            FastImageFile fastImageFile = new FastImageFile(inputStream, file.length(), "png", null);
            StorePath storePath = fastFileStorageClient.uploadImage(fastImageFile);

           LogHelper.info( storePath.getFullPath());
            LogHelper.info( storePath.getGroup());

            response.put("code", 0);

        } catch (Exception e) {

        }
        return response;
    }

    @GetMapping(value = "DataCollectServer/rtuData/waterLevelList")
    @ResponseBody
    public JSONObject waterLevelList() {
        JSONObject response = new JSONObject();
        try {
            JSONArray datas = new JSONArray();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Map<String, Object>> list = this.rtuDataService.waterLevelList();
            if (null != list && list.size() > 0) {
                int idx = 1;
                for (Map<String, Object> m : list) {
                    JSONObject d = new JSONObject();
                    d.put("indexNumber", String.format("%d", idx));
                    d.put("rtuCode", m.get("rtuCode"));
                    d.put("rtuName", m.get("rtuName"));
                    BigDecimal wl = (BigDecimal) m.get("waterLevel");
                    if (null != wl) {
                        d.put("waterLevel", wl.toPlainString());
                    } else {
                        d.put("waterLevel", "");
                    }
                    Timestamp tm = (Timestamp) m.get("collectTm");
                    if (null != tm) {
                        d.put("tm", timeFormat.format(new Date(tm.getTime())));
                    } else {
                        d.put("tm", "");
                    }
                    datas.add(d);
                    idx += 1;
                }
            }
            response.put("data", datas);
            response.put("code", "0");
        } catch (Exception e) {
            response.put("code", "1");
        }
        return response;
    }

}
