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

namespace project_flux.API
{
    public class Job
    {
        //INITIAL CLASS
        public Guid Id { get; set; }
        public Guid BoardId { get; set; }
        public String Name { get; set; }

        //GET FUNCTIONS
        public static async Task<List<Job>> GetJobsByBoardId(Guid BoardId)
        {
            var _get = await Connection.Get.ParsedData("board/" + BoardId + "/jobs");
            String _status = _get[0]["status"].ToString().ToLower();
            List<Job> _job = new List<Job>();
            if (_status == "ok")
            {
                Boolean _firstItem = true;
                foreach (JObject _item in _get)
                {
                    if (_firstItem)
                        _firstItem = !_firstItem;
                    else
                    {
                        Job _newJob = new Job();
                        _newJob.Id = Guid.Parse(_item["Id"].ToString());
                        _newJob.Name = _item["Name"].ToString();
                        _newJob.BoardId = Guid.Parse(_item["BoardId"].ToString());
                        _job.Add(_newJob);
                    }
                }
            }
            else
            {
                Job _newJob = new Job();
                _job.Add(_newJob);
            }
            return _job;
        }
        public static async Task<Job> GetJobByID(Guid Id)
        {
            var _get = await Connection.Get.ParsedData("job/" + Id);
            String _status = _get[0]["status"].ToString().ToLower();
            Job _job = new Job();
            if (_status == "ok")
            {
                _job.Id = Guid.Parse(_get[1]["Id"].ToString());
                _job.Name = _get[1]["Name"].ToString();
                _job.BoardId = Guid.Parse(_get[1]["BoardId"].ToString());
            }
            return _job;
        }

        //POST FUNCTIONS
        public static async Task<String> Add(Guid BoardId, String Name)
        {
            var JSON = new Dictionary<string, object>();
            JSON["Name"] = Name;
            JSON["BoardId"] = BoardId.ToString();

            return await Connection.Post.PostAndReturnResult("job/", JsonConvert.SerializeObject(JSON));
        }

        //PUT FUNCTIONS
        public static async Task<bool> Update(Guid Id, Guid BoardId, String Name)
        {
            var JSON = new Dictionary<string, object>();
            JSON["Name"] = Name;
            JSON["BoardId"] = BoardId;

            if (await Connection.Put.PutAndReturnResult("job/" + Id.ToString(), JsonConvert.SerializeObject(JSON)))
                return true;
            return false;
        }

        //DELETE FUNCTIONS
        public static async Task<bool> Archive(Guid Id)
        {
            if (await Connection.Delete.DeleteAndReturnResult("job/" + Id.ToString()))
                return true;
            return false;
        }
    }
}
