package com.rehome.mqttclienttemperature.service.impl;

import com.google.gson.Gson;
import com.rehome.mqttclienttemperature.dao.TemperatureRepository;
import com.rehome.mqttclienttemperature.dto.ResponseDto;
import com.rehome.mqttclienttemperature.entity.Temperature;
import com.rehome.mqttclienttemperature.service.OracleTemperatureService;
import com.rehome.mqttclienttemperature.service.TemperatureService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class TemperatureServiceImpl implements TemperatureService {

    @Resource
    private TemperatureRepository temperatureRepository;
    @Resource
    private OracleTemperatureService oracleTemperatureService;

    @Override
    public void saveTemperature(String temperatureAndHumidityData,String topic) {
        if(temperatureAndHumidityData!=null&&temperatureAndHumidityData.length()>0){
            String[] strDataTemperature = temperatureAndHumidityData.split(" ");
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
            SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String nowDate = sdf.format(now);
            String nowHour = sdfHour.format(now);
            String dataMinute = sdfMinute.format(now);
            String dataSecond = sdfSecond.format(now);
            Temperature temperature = new Temperature();
            temperature.setCreateDate(now);
            temperature.setDataDate(nowDate);
            temperature.setDataHour(nowHour);
            temperature.setDataMinute(dataMinute);
            if(topic!=null){
                temperature.setTopic(topic);
                if(topic.equals("WifiSHT/7C87CE9CA4E6/SHT20")){
                    temperature.setLocationDesc("广东省珠海市高新区唐家湾镇东岸村水风三街28号501");
                }
                if(topic.equals("WifiSHT/7C87CE9F5CBF/SHT20")){
                    temperature.setLocationDesc("广东省珠海市金湾区三灶镇百川路1号1栋1单元1508房");
                }
                if(topic.equals("WifiSHT/4CEBD686B6AA/SHT20")){
                    temperature.setLocationDesc("广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号");
                }
            }
            temperature.setHumidity(strDataTemperature[0]);
            temperature.setTemperature(strDataTemperature[1]);

            this.temperatureRepository.save(temperature);
//            ResponseDto responseDto = this.oracleTemperatureService.saveTemperature(temperature);
//            log.info(new Gson().toJson(responseDto));
        }
    }

    @Override
    public void saveTemperature(Temperature temperature) {
        this.temperatureRepository.save(temperature);
    }

    @Override
    public List<Temperature> findFirst10ByDataDateAndLocationDesc(String dataDate, String locationDesc) {
        int page=1,size=10;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);

//        userRepository.findALL(pageable);
//        userRepository.findByUserName("testName", pageable);

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String nowDate = sdf.format(now);

        Optional<List<Temperature>> listResult = Optional.ofNullable(temperatureRepository.findFirst10ByDataDateAndLocationDesc(dataDate, locationDesc, sort));
        if(listResult.isPresent()){
            return listResult.get();
        }
        return new ArrayList<>();
    }

    @Override
    public List<Temperature> findFirst10ByLocationDesc(String locationDesc) {
        int page=1,size=10;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);

//        userRepository.findALL(pageable);
//        userRepository.findByUserName("testName", pageable);

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String nowDate = sdf.format(now);

        Optional<List<Temperature>> listResult = Optional.ofNullable(temperatureRepository.findFirst10ByDataDateAndLocationDesc(nowDate, locationDesc, sort));
        if(listResult.isPresent()){
            return listResult.get();
        }
        return new ArrayList<>();
    }

    @Override
    public Temperature findFirstByOrderByLocationDescAsc(String locationDesc) {
        Optional<Temperature> temperatureResult = Optional.ofNullable(temperatureRepository.findFirstByOrderByDataDateDesc());
        if(temperatureResult.isPresent()){
            return temperatureResult.get();
        }
        return null;
    }

    @Override
    public List<Temperature> queryFirst10ByDataDateAndLocationDesc(String dataDate, String locationDesc) {
        int page=0,size=10;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<Temperature> listResult = temperatureRepository.queryFirst10ByDataDateAndLocationDesc(dataDate, locationDesc, pageable);

        if(listResult.getTotalPages()>0){
            return listResult.getContent();
        }

        return new ArrayList<>();
    }


}
