package org.jeecg.sy.scala

import com.alibaba.fastjson.{JSONArray, JSONObject}
import org.apache.spark.sql.SparkSession
import org.jeecg.sy.java.{getData, weather_1, weather_2}

import java.text.{ParseException, SimpleDateFormat}
import java.util
import java.util.{ArrayList, Collections, Comparator, HashMap, List, Map}

object analysis {
  // 数据爬取，输入一个List[weather_1]，返回处理完的数据
  def getData(list: List[weather_1]): List[weather_1] = {
    // 将list 倒序// 将list 倒序
    for (i <- 0 until list.size / 2) {
      val temp: weather_1 = list.get(i)
      list.set(i, list.get(list.size - i - 1))
      list.set(list.size - i - 1, temp)
    }
    return list
  }

  def analysis2(list: List[weather_1]): List[weather_1] = {
    // 先将list按照时间排序，旧的在前面，新的在后面
    val sdf: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    Collections.sort(list, new Comparator[weather_1]() {
      override def compare(d1: weather_1, d2: weather_1): Int = {
        try return sdf.parse(d1.getData).compareTo(sdf.parse(d2.getData))
        catch {
          case e: ParseException =>
            throw new IllegalArgumentException(e)
        }
      }
    })
    list
  }

  def analysis3(list: List[weather_2]): List[weather_2] = {
    list
  }

  // 获取列表中的 最高气温和最低气温的变化曲线（两条线）
  def getTemperature(list: util.List[weather_1]): JSONObject = {
    val result = new JSONObject
    // 最高气温
    val highTemperature = new JSONArray
    // 最低气温
    val lowTemperature = new JSONArray
    for (i <- 0 until list.size) {
      val temp1 = new JSONObject
      // 格式为 date + temperature
      //为data加上年份2024，然后转为日期模式
      val data = list.get(i).getData
      temp1.put("date", data)
      temp1.put("temperature", list.get(i).getHighTemperature.replaceAll("℃", ""))
      highTemperature.add(temp1)
      val temp2 = new JSONObject
      temp2.put("date", data)
      temp2.put("temperature", list.get(i).getLowTemperature.replaceAll("℃", ""))
      lowTemperature.add(temp2)
    }
    // 最高气温
    result.put("highTemperature", highTemperature)
    // 最低气温
    result.put("lowTemperature", lowTemperature)
    result
  }

  // 获取列表中 风力+风向这对字符串的出现次数（并排序）
  def getWind(list: util.List[weather_1]): JSONObject = {
    val result = new JSONObject
    // 风力+风向
    val wind = new util.HashMap[String, Integer]
    for (i <- 0 until list.size) {
      val windSpeed = list.get(i).getWindSpeed
      if (wind.containsKey(windSpeed)) wind.put(windSpeed, wind.get(windSpeed) + 1)
      else wind.put(windSpeed, 1)
    }
    // 对wind进行排序
    val list1 = new util.ArrayList[util.Map.Entry[String, Integer]](wind.entrySet)
    list1.sort(util.Map.Entry.comparingByValue)
    // 塞入JSONArray
    val winds = new JSONArray
    val counts = new JSONArray
    result.put("wind", winds)
    result.put("count", counts)
    result
  }

  // 天气类型出现次数饼图
  def getTemperatureMap(list: util.List[weather_1]): JSONArray = {
    val temperatureMapArray = new JSONArray
    val wind = new util.HashMap[String, Integer]
    for (i <- 0 until list.size) {
      val windSpeed = list.get(i).getDayOrNight.split("/")(0)
      if (wind.containsKey(windSpeed)) wind.put(windSpeed, wind.get(windSpeed) + 1)
      else wind.put(windSpeed, 1)
    }
    // 对wind进行排序
    val list1 = new util.ArrayList[util.Map.Entry[String, Integer]](wind.entrySet)
    list1.sort(util.Map.Entry.comparingByValue)
    temperatureMapArray
  }
}
