using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace Siger.Middlelayer.Common.Helpers
{
    public class JArryHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<List<object>> FormatList(List<string> list, int count)
        {
            if (list == null || list.Count == 0)
                return new List<List<object>>();

            var result = new List<List<object>>();

            if (list.Count > count)
            {
                int j = 0;
                for (int i = list.Count - count; i < list.Count; i++)
                {
                    result.Add(new List<object>() {
                        j,list[i]});
                    j += 1;
                }
                return result;
            }

            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    i,list[i]
                });
            }
            return result;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<List<object>>ArryFormat(JArray list,int count)
        {
            if (list == null || list.Count == 0)
                return new List<List<object>>();

            var result = new List<List<object>>();

            if (list.Count > count)
            {
                int j = 0;
                for (int i = list.Count - count; i < list.Count; i++)
                {
                    result.Add(new List<object>() {
                    j,list[i]});
                    j += 1;
                }
                return result;
            }
            
            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    i,list[i]
                });
            }
            return result;
        }

        public static string[] ArryLastCount(string[] arry, int count)
        {

            if (arry.Length > count)
            {
                string[] newArry = new string[count];
                Array.ConstrainedCopy(arry, arry.Length - count, newArry, 0, count);
                return newArry;
            }
            else
            {
                return arry;
            }

        }
        public static List<List<object>> ArryFormatReson(JArray list)
        {
            if (list == null || list.Count == 0)
                return new List<List<object>>();

            var result = new List<List<object>>();

            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    list[i],""
                });
            }
            return result;
        }
        public static List<List<object>> ArryFormat(JArray list)
        {
            if (list == null || list.Count == 0)
                return new List<List<object>>();

            var result = new List<List<object>>();

            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    i,list[i]
                });
            }
            return result;
        }
        public static List<List<object>> ArryFormat(List<string> list)
        {
            if (list == null || list.Count == 0)
                return new List<List<object>>();

            var result = new List<List<object>>();

            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    i,list[i]
                });
            }
            return result;
        }
        public static List<List<object>> ArryFormat(List<object> list)
        {
            var result = new List<List<object>>();
            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new List<object>() {
                    i,list[i]
                });
            }
            return result;
        }
    }
}
