@using System.ComponentModel
@using Newtonsoft.Json
@using System.Text; 
@model Tuple<Channel, Enumerations.LewdnessFilterLevel, Enumerations.MeannessFilterLevel>
@{
  var ThisChannel = Model.Item1;
  var IfInheritedLewdnessFilterLevel = Model.Item2;
  var IfInheritedMeannessFilterLevel = Model.Item3;
}

<a href="/">home</a>/
@Html.Raw(ViewData["breadcrumbs"])

<table class="table">
  <tbody>
    <tr>
      <th scope="row">Display Name</th>
      <td>@ThisChannel.DisplayName</td>
    </tr>
    <tr>
      <th scope="row">Channel type</th>
      <td>@(Enumerations.GetDescription(ThisChannel.ChannelType))</td>
    </tr>
    <tr>
      <th scope="row">Lewdness Filter Level</th>
      <td>
        <select name="LewdnessFilterLevel" id="LewdnessFilterLevel" onchange="patchModel(jsonifyChannel(), '/api/Channels/')">
          <!option value="" @(ThisChannel.LewdnessFilterLevel == null ? "selected" : "")>⤵ inherited - @Enumerations.GetDescription(IfInheritedLewdnessFilterLevel)</!option>
          @foreach (Enumerations.LewdnessFilterLevel enumVal in
          Enum.GetValues(typeof(Enumerations.LewdnessFilterLevel)))
          {
            <!option value="@((int)enumVal)" @(ThisChannel.LewdnessFilterLevel == enumVal ? "selected" : "")>
              @(Enumerations.GetDescription<Enumerations.LewdnessFilterLevel>(enumVal))</!option>
          }
        </select>
      </td>
    </tr>
    <tr>
      <th scope="row">Links Allowed</th>
      <td>@(ThisChannel.LinksAllowed?.ToString() ?? "unknown")</td>
    </tr>
    <tr>
      <th scope="row">Lineage summary</th>
      <td>@ThisChannel.LineageSummary</td>
    </tr>
    <tr>
      <th scope="row">max attachment bytes</th>
      <td>@ThisChannel.MaxAttachmentBytes (i hear there's "ByteSize")</td>
    </tr>
    <tr>
      <th scope="row">max message length</th>
      <td>@(ThisChannel.MaxTextChars?.ToString() ?? "inherited")</td>
    </tr>
    <tr>
      <th scope="row">Meanness Filter Level</th>
      <td>
        <select name="MeannessFilterLevel" id="MeannessFilterLevel" onchange="patchModel(jsonifyChannel(), '/api/Channels/')">
          <!option value="" @(ThisChannel.MeannessFilterLevel == null ? "selected" : "")>⤵ inherited - @Enumerations.GetDescription(IfInheritedMeannessFilterLevel)</!option>
          @foreach (Enumerations.MeannessFilterLevel enumVal in
          Enum.GetValues(typeof(Enumerations.MeannessFilterLevel)))
          {
            <!option value="@((int)enumVal)" @(ThisChannel.MeannessFilterLevel == enumVal ? "selected" : "")>
              @(Enumerations.GetDescription<Enumerations.MeannessFilterLevel>(enumVal))</!option>
          }
        </select>
      </td>
    </tr>
    <tr>
      <th scope="row">Messages (count)</th>
      <td>@(ThisChannel.Messages?.Count ?? 0)</td>
    </tr>
    <tr>
      <th scope="row">Protocol</th>
      <td>@ThisChannel.Protocol</td>
    </tr>
    <tr>
      <th scope="row">Reactions Possible</th>
      <td>@(ThisChannel.ReactionsPossible?.ToString() ?? "inherited")</td>
    </tr>
    <tr>
      <th scope="row">Sub Channels</th>
      <td>
        @if((ThisChannel.SubChannels?.Count ?? 0) > 0)
        {
          @Html.Raw("<div id=\"channelsTree\"></div>");
        }
        else
        {
          @Html.Raw("0")
        }
      </td>
    </tr>
    <tr>
      <th scope="row">Accounts</th>
      <td>
        @if((ThisChannel.Users?.Count ?? 0) > 0)
        {
          @Html.Raw("<div id=\"accountsTree\"></div>");
        }
        else
        {
          @Html.Raw("none")
        }
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <button onclick="forget()">forget</button>
      </td>
    </tr>
  </tbody>
</table>

@section Scripts{
  <script type="text/javascript">
    @{
      var modelAsString = JsonConvert.SerializeObject(ThisChannel, new JsonSerializerSettings
      {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
      });
    }
      const channelOnLoad = @Html.Raw(modelAsString);
    function jsonifyChannel() {
      var channelNow = structuredClone(channelOnLoad);
      channelNow.SubChannels = null;
      channelNow.ParentChannel = null;
      channelNow.Messages = null;
      channelNow.Users = null;

      channelNow.LewdnessFilterLevel = document.querySelector("#LewdnessFilterLevel").value;
      channelNow.MeannessFilterLevel = document.querySelector("#MeannessFilterLevel").value;
      console.log(channelNow);
      return channelNow;
    }
    function forget(){
      console.log("here we go");
      if(window.confirm("delete? really really?") == true){
        deleteModel(jsonifyChannel(), '/api/Channels/');
      }
    }
    
    function channelsTree() {
      var tree = @Html.Raw(ViewData["channelsTree"]);
      return tree;
    }

    function accountsTree() {
      @{
        var sb = new StringBuilder();
        sb.Append("[{text: \"accounts\", \"expanded\":true, nodes: [");
        var first = true;
        foreach (var acc in ThisChannel.Users.OrderBy(a => a.SeenInChannel.LineageSummary))
        {
            if(!first)
                sb.Append(','); 
            sb.Append($"{{text: \"<div class=\\\"account {acc.Protocol}\\\"><div class=\\\"protocol-icon\\\">&nbsp;</div>{acc.SeenInChannel.LineageSummary}/<a href=\\\"/Accounts/Details/{acc.Id}\\\">{acc.DisplayName}</a>\"}}");
            first=false;
        }
        sb.Append("]}]");
      }
      //console.log(@Html.Raw(sb.ToString()));
      var tree = @Html.Raw(sb.ToString());
      return tree;
    }
    $('#channelsTree').bstreeview({ data: channelsTree() });
    $('#accountsTree').bstreeview({ data: accountsTree() });

  </script>
}