﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entities.Slack
{
    public class SlashCommand
    {
        public string token { get; set; }
        
        public string team_id { get; set; }
        
        public string team_domain { get; set; }
        
        public string channel_id { get; set; }
        
        public string channel_name { get; set; }
        
        public string user_id { get; set; }
        
        public string user_name { get; set; }
        
        public string command { get; set; }
        
        public string text { get; set; }
        
        public string response_url { get; set; }

        public Entities.Merge ToMerge()
        {
            // /merge -q:#10 -s:s9yv4 -t:qatv4 -m:Additional message
            List<string> args = this.text.Split('-').ToList();

            Entities.Merge merge = new Entities.Merge();

            foreach (string arg in args)
            {
                string lowerCase = arg.ToLower();

                if (lowerCase.Contains("q:"))
                {
                    merge.Query = arg.Split(':').Last().Trim();
                }
                else if (lowerCase.Contains("s:"))
                {
                    merge.Source = arg.Split(':').Last().Trim();
                }
                else if (lowerCase.Contains("t:"))
                {
                    merge.Target = arg.Split(':').Last().Trim();
                }
                else if (lowerCase.Contains("m:"))
                {
                    merge.Message = arg.Split(':').Last().Trim();
                }
            }

            if (!merge.Verify())
            {
                throw new ArgumentException("-q:,-s:,-t: are required arguments");
            }

            return merge;
        }
    }
}
