package com.rehome.mqttclienttemperature.service.impl;

import com.rehome.mqttclienttemperature.dao.TemperatureRepository;
import com.rehome.mqttclienttemperature.dao.WebServiceCityRepository;
import com.rehome.mqttclienttemperature.dao.WebServiceProvinceRepository;
import com.rehome.mqttclienttemperature.dao.WebServiceWeatherInfoRepository;
import com.rehome.mqttclienttemperature.entity.Temperature;
import com.rehome.mqttclienttemperature.entity.WebServiceCity;
import com.rehome.mqttclienttemperature.entity.WebServiceProvince;
import com.rehome.mqttclienttemperature.entity.WebServiceWeatherInfo;
import com.rehome.mqttclienttemperature.service.TemperatureService;
import com.rehome.mqttclienttemperature.service.WebServiceWeatherService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class WebServiceWeatherServiceImpl implements WebServiceWeatherService {

    @Resource
    private WebServiceProvinceRepository webServiceProvinceRepository;
    @Resource
    private WebServiceCityRepository webServiceCityRepository;
    @Resource
    private WebServiceWeatherInfoRepository webServiceWeatherInfoRepository;

    @Override
    public void saveWebServiceProvince(WebServiceProvince webServiceProvince) {
        this.webServiceProvinceRepository.save(webServiceProvince);
    }

    @Override
    public WebServiceProvince findProvinceByProvinceName(String provinceName) {
        Optional<WebServiceProvince> result = Optional.ofNullable(webServiceProvinceRepository.findByProvinceName(provinceName));
        if(result.isPresent()){
            return result.get();
        }
        return null;
    }

    @Override
    public void saveWebServiceCity(WebServiceCity webServiceCity) {
        this.webServiceCityRepository.save(webServiceCity);
    }

    @Override
    public WebServiceCity findByCityName(String cityName) {
        Optional<WebServiceCity> result = Optional.ofNullable(webServiceCityRepository.findByCityName(cityName));
        if(result.isPresent()){
            return result.get();
        }
        return null;
    }

    @Override
    public WebServiceCity findByCode(String code) {
        Optional<WebServiceCity> result = Optional.ofNullable(webServiceCityRepository.findByCode(code));
        if(result.isPresent()){
            return result.get();
        }
        return null;
    }

    @Override
    public List<WebServiceCity> findAllCity() {
        Optional<List<WebServiceCity>> result = Optional.ofNullable(webServiceCityRepository.findAll());
        if(result.isPresent()){
            return result.get();
        }
        return new ArrayList<>();
    }

    @Override
    public void saveWebServiceWeatherInfo(WebServiceWeatherInfo webServiceWeatherInfo) {
        this.webServiceWeatherInfoRepository.save(webServiceWeatherInfo);
    }

    @Override
    public List<WebServiceWeatherInfo> findByCreateDateAndCityCode(String createDate, String cityCode) {
        Optional<List<WebServiceWeatherInfo>> result = Optional.ofNullable(webServiceWeatherInfoRepository.findByCreateDateAndCityCode(createDate,cityCode));
        if(result.isPresent()){
            return result.get();
        }
        return new ArrayList<>();
    }

    @Override
    public List<WebServiceWeatherInfo> findByCreateDateAndCityName(String createDate, String cityName) {
        Optional<List<WebServiceWeatherInfo>> result = Optional.ofNullable(webServiceWeatherInfoRepository.findByCreateDateAndCityName(createDate,cityName));
        if(result.isPresent()){
            return result.get();
        }
        return new ArrayList<>();
    }
}
