using System;
using System.Linq;
using StubCore.Plugins.Logging;
using System.Collections.Generic;
using StubCore.Plugins.Repositories;
using System.Runtime.InteropServices;
using StubCore.Plugins.DataOperations;

namespace StubCore.Plugins.DataCollector
{
    public class FrontCollector
    {
        private readonly string _name;
        private readonly string _folder;
        private readonly bool _isRootLevel;
        public readonly CollectorsLogger _logger = new CollectorsLogger();

        public FrontCollector(string sourceName, string folder, [Optional, DefaultParameterValue(false)] bool isRootLevel)
        {
            _name = sourceName;
            _folder = folder;
            _isRootLevel = isRootLevel;
        }

        public List<T> GetFiles<T>()
        {
            var _list = new List<T>();
            var _data = new DataRepository(_name, _folder, _isRootLevel);

            var files = _data.Get();

            if (!String.IsNullOrEmpty(files))
            {
                var _json = new JsonOperations();
                _list = _json.GetDataObject<List<T>>(files);
            }
            else
            {
                _logger.ProfileListNotExists(_folder);
            }

            return _list;
        }

        public T GetItem<T>(int id)
        {
            var _file = default(T);
            var _list = GetFiles<T>();

            if (_list.Count > 0)
            {
                _file = _list.Find(item => (int)item.GetType().GetProperty("id").GetValue(item) == id);
            }
            else
            {
                _logger.ItemWasNotFound<T>(_file, id);
            }

            return _file;
        }

        public bool SaveRow<T>(T input)
        {
            var _list = GetFiles<T>();
            _list.Add(input);

            return UpdateListData<List<T>>(_list);
        }

        public bool UpdateRow<T>(T input)
        {
            var isUpdated = true;
            var _list = GetFiles<T>();

            var _item = _list.Find(item => (int)item.GetType().GetProperty("id").GetValue(item) == (int)input.GetType().GetProperty("id").GetValue(input));

            if (_item != null)
            {
                var index = _list.IndexOf(_item);
                _list.RemoveAt(index);
                _list.Insert(index, input);

                isUpdated = UpdateListData<List<T>>(_list);
            }
            else
            {
                isUpdated = false;
                _logger.UpdateObjectNotFound((int)input.GetType().GetProperty("id").GetValue(input), _folder);
            }

            return isUpdated;
        }

        public bool DeleteRow<T>(int id)
        {
            var isDeleted = false;
            var _list = GetFiles<T>();
            var result = _list.RemoveAll(item => (int)item.GetType().GetProperty("id").GetValue(item) == id);

            if (result > 0)
            {
                isDeleted = UpdateListData<List<T>>(_list);
            }
            else
            {
                _logger.DeleteObjectNotFound(id, _folder);
            }

            return isDeleted;
        }

        public bool UpdateListData<T>(T list)
        {
            var _json = new JsonOperations();
            var _files = new DataRepository(_name, _folder, _isRootLevel);

            var _newList = _json.GetJsonString<T>(list);
            return _files.UpdateProfileList(_newList);
        }
    }
}
