package com.rehome.jpahefengweather.service.Impl;

import com.rehome.jpahefengweather.dao.CityRepository;
import com.rehome.jpahefengweather.dao.ProvinceRepository;
import com.rehome.jpahefengweather.entity.HefengCity;
import com.rehome.jpahefengweather.entity.HefengProvince;
import com.rehome.jpahefengweather.service.CityService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CityServiceImpl implements CityService {
    @Resource
    private CityRepository cityRepository;

    @Resource
    private ProvinceRepository provinceRepository;

    @Override
    public void saveProvinceList(List<HefengProvince> provinceList) {
        Date nowTime = new Date();
        for (HefengProvince province : provinceList) {
            HefengProvince provinceDb = this.provinceRepository.findByCode(province.getCode());
            if (provinceDb != null) {
                provinceDb.setName(province.getName());
                provinceDb.setLastUpdateDate(nowTime);
                this.provinceRepository.save(provinceDb);
            }else{
                province.setLastUpdateDate(nowTime);
                province.setCreateDate(nowTime);
                this.provinceRepository.save(province);
            }
        }
    }

    @Override
    public void saveCityList(List<HefengCity> cityList) {
        Date now = new Date();
        for (HefengCity city : cityList) {
            HefengCity cityDB = this.cityRepository.findByLocationId(city.getLocationId());
            if (cityDB != null) {
                cityDB.setLocationNameEn(city.getLocationNameEn());
                cityDB.setLocationNameZh(city.getLocationNameZh());
                cityDB.setCountryCode(city.getCountryCode());
                cityDB.setCountryNameEn(city.getCountryNameEn());
                cityDB.setCountryNameZh(city.getCountryNameZh());
                cityDB.setAdm1NameEn(city.getAdm1NameEn());
                cityDB.setAdm1NameZh(city.getAdm1NameZh());
                cityDB.setAdm2NameEn(city.getAdm2NameEn());
                cityDB.setAdm2NameZh(city.getAdm2NameZh());
                cityDB.setTimezone(city.getTimezone());
                cityDB.setLatitude(city.getLatitude());
                cityDB.setLongitude(city.getLongitude());
                cityDB.setAdcode(city.getAdcode());
                cityDB.setLastUpdateDate(now);
                this.cityRepository.save(cityDB);
            } else {
                city.setLastUpdateDate(now);
                city.setCreateDate(now);
                this.cityRepository.save(city);
            }
        }
    }

    @Override
    public List<HefengCity> findAllCity() {
        return this.cityRepository.findAll();
    }

    @Override
    public List<HefengCity> findCityByProvinceCode(String provinceCode) {
        return this.cityRepository.findByAdm1NameEn(provinceCode);
    }

    @Override
    public List<HefengCity> findCityByCityCode(String cityCode) {
        return this.cityRepository.findByAdm2NameEn(cityCode);
    }

    @Override
    public List<HefengCity> findByLocationNameEn(String locationNameEn) {
        return this.cityRepository.findByLocationNameEn(locationNameEn);
    }

    @Override
    public List<HefengProvince> findAllProvince() {
        return this.provinceRepository.findAll();
    }
}
