package net.dirtydeeds.discordsoundboard.web;

import net.dirtydeeds.discordsoundboard.service.SoundboardBot;
import net.dirtydeeds.discordsoundboard.service.SoundboardDispatcher;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
public class MessageController {

  @Autowired
  SoundboardDispatcher dispatcher;

  @RequestMapping(value = "/bots")
  public ResponseEntity<Object> getBots() {
    List<String> names = new LinkedList<>();
    for (SoundboardBot bot : dispatcher.getBots()) {
      names.add(bot.getBotName());
    }
    return new ResponseEntity<>(names, HttpStatus.OK);
  }

  @RequestMapping(value = "/bots/{id}/servers")
  public ResponseEntity<Object> getServers(@PathVariable("id") String id) {
    SoundboardBot bot = dispatcher.getBots().get(Integer.parseInt(id));
    return new ResponseEntity<>(bot.getGuilds(), HttpStatus.OK);
  }

  @RequestMapping(value = "/bots/{id}/servers/{sid}/channels")
  public ResponseEntity<Object> getTextChannels(@PathVariable("id") String id, @PathVariable("sid") String sid) {
    SoundboardBot bot = dispatcher.getBots().get(Integer.parseInt(id));
    Guild guild = bot.getGuilds().get(Integer.parseInt(sid));
    return new ResponseEntity<>(guild.getTextChannels(), HttpStatus.OK);
  }

  @RequestMapping(value = "/bots/{id}/servers/{sid}/channels/{cid}/messages", method = RequestMethod.POST)
  public ResponseEntity<Object> sendMessageToChannel(@PathVariable("id") String id, @PathVariable("sid") String sid, @PathVariable("cid") String cid, @RequestBody String msg) {
    SoundboardBot bot = dispatcher.getBots().get(Integer.parseInt(id));
    Guild guild = bot.getGuilds().get(Integer.parseInt(sid));
    TextChannel c = guild.getTextChannels().get(Integer.parseInt(cid));
    c.sendMessage(msg).queue();
    return new ResponseEntity<>("Message sent", HttpStatus.OK);
  }

}

@@ -1023,24 +1023,69 @@
 tring id) %7B%0A
+    List%3CString%3E names = new LinkedList%3C%3E();%0A
     Soundboa
@@ -1140,24 +1140,106 @@
 seInt(id));%0A
+    for (Guild guild : bot.getGuilds()) %7B%0A      names.add(guild.getName());%0A    %7D%0A
     return n
@@ -1258,31 +1258,21 @@
 ntity%3C%3E(
-bot.getGuilds()
+names
 , HttpSt
@@ -1459,24 +1459,69 @@
 ring sid) %7B%0A
+    List%3CString%3E names = new LinkedList%3C%3E();%0A
     Soundboa
@@ -1638,24 +1638,112 @@
 eInt(sid));%0A
+    for (TextChannel c : guild.getTextChannels()) %7B%0A      names.add(c.getName());%0A    %7D%0A
     return n
@@ -1762,39 +1762,21 @@
 ntity%3C%3E(
-guild.getTextChannels()
+names
 , HttpSt

Fix serializing issue