import React from 'react';
import { fetchGet } from '~/Utils/fetch';

export const applyWeatherName = (data) => {
  if (data.Pm10 !== null) {
    if (data.Pm10 >= 0 && data.Pm10 <= 30) {
      data.Pm10Grade = '좋음';
    } else if (data.Pm10 >= 31 && data.Pm10 <= 80) {
      data.Pm10Grade = '보통';
    } else if (data.Pm10 >= 81 && data.Pm10 <= 150) {
      data.Pm10Grade = '나쁨';
    } else if (data.Pm10 >= 151) {
      data.Pm10Grade = '매우나쁨';
    }
  }
  if (data.Pm25 !== null) {
    if (data.Pm25 >= 0 && data.Pm25 <= 15) {
      data.Pm25Grade = '좋음';
    } else if (data.Pm25 >= 16 && data.Pm25 <= 35) {
      data.Pm25Grade = '보통';
    } else if (data.Pm25 >= 36 && data.Pm25 <= 75) {
      data.Pm25Grade = '나쁨';
    } else if (data.Pm25 >= 76) {
      data.Pm25Grade = '매우나쁨';
    }
  }
  if (data.RainType == 1) {
    data.Weather = '비';
  } else if (data.RainType == 2) {
    data.Weather = '눈비';
  } else if (data.RainType == 3) {
    data.Weather = '눈';
  } else if (data.RainType == 4) {
    data.Weather = '소나기';
  } else {
    if (data.Sky == 0) {
      data.Weather = '보통';
    } else if (data.Sky == 1) {
      data.Weather = '맑음';
    } else if (data.Sky == 2) {
      data.Weather = '구름조금';
    } else if (data.Sky == 3) {
      data.Weather = '구름많음';
    } else if (data.Sky == 4) {
      data.Weather = '흐림';
    }
  }
  if (data.WindAngle !== null) {
    const dir = Math.round(data.WindAngle / 45);
    if (dir == 1) {
      data.WindDirection = '북동';
    } else if (dir == 2) {
      data.WindDirection = '동';
    } else if (dir == 3) {
      data.WindDirection = '남동';
    } else if (dir == 4) {
      data.WindDirection = '남';
    } else if (dir == 5) {
      data.WindDirection = '남서';
    } else if (dir == 6) {
      data.WindDirection = '서';
    } else if (dir == 7) {
      data.WindDirection = '북서';
    } else {
      data.WindDirection = '북';
    }
  } else {
    data.WindDirection = '';
  }
  return data;
}

export const fetchWather = ({sido='', sigun=''},success, failure) => {
  // 날씨
  fetchGet(`/api/SiteWeatherInfo?sido=${sido}&sigun=${sigun}`)
    .then(res => {
      let data = res;
      data = applyWeatherName(data);
      console.log(data);
      success(data);
    })
    .catch(e => {
      failure(e);
    });
};

export default fetchWather;
