package gavilan.irc.webmvc;

import gavilan.irc.Cron;
import gavilan.irc.Features;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeaturesController {

private final Features features;
private final Cron cron;

@Autowired
public FeaturesController(Features features, Cron cron) {
  this.features = features;
  this.cron = cron;
}

@RequestMapping(value = "/get/features", method = RequestMethod.GET)
public Map<String, Boolean> features() {
  return features.getFeatures();
}

@RequestMapping(value = "/command/toggle/{feature}", method = RequestMethod.GET)
public Map<String, Boolean> toggle(@PathVariable String feature) {
  features.setFeature(feature, !features.hasFeature(feature));
  if ("cron".equals(feature)) {
    cron.updateCron();
  }
  return features.getFeatures();
}

}
