package com.lichader.alfred.type;

import com.lichader.alfred.metroapi.v3.DisruptionService;
import com.lichader.alfred.metroapi.v3.RouteService;
import com.lichader.alfred.metroapi.v3.RouteTypeService;
import com.lichader.alfred.metroapi.v3.model.Disruption;
import com.lichader.alfred.metroapi.v3.model.DisruptionsResponse;
import com.lichader.alfred.metroapi.v3.model.Route;
import com.lichader.alfred.metroapi.v3.model.RouteResponse;
import com.lichader.alfred.slack.MessageBot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MetroAlfred {

    private static final String LINE_BREAK = "%0A"; // url encoded \n

    @Autowired
    private RouteService routeService;

    @Autowired
    private RouteTypeService routeTypeService;

    @Autowired
    private DisruptionService disruptionService;

    @Autowired
    private MessageBot messageBot;


//    @Scheduled(cron = "")
    @Scheduled(fixedRate = 5000)
    public void checkDisruption(){
        RouteResponse allRoutes = routeService.getAll();
        Route hurstbridgeRoute = allRoutes.Routes
                .stream()
                .filter(o -> o.RouteName.equalsIgnoreCase("Hurstbridge")).findFirst().get();
        DisruptionsResponse allDisruptions = disruptionService.getDisruption(hurstbridgeRoute.RouteId);

        List<Disruption> disruptions = allDisruptions.disruptions.MetroTrain;

        for (Disruption dis : disruptions){
            StringBuilder sb = new StringBuilder();
            sb.append("Disruption: ").append(dis.Description).append(LINE_BREAK);
            sb.append("Status: ").append(dis.DisruptionStatus).append(LINE_BREAK);
            sb.append("Type: ").append(dis.DisruptionType).append(LINE_BREAK);
            sb.append("From " + dis.FromDate + " to " + dis.ToDate);

            messageBot.send(sb.toString());
        }
    }
}

@@ -1022,67 +1022,90 @@
 ;%0A%0A%0A
-//    @Scheduled(cron = %22%22)%0A    @Scheduled(fixedRate = 5000
+    // Runs in the mid night of every day%0A    @Scheduled(cron = %220 0 12 1/1 * ? *%22
 )%0A  

Make the check-disruption service run in the midnight daily