using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Console = HyperCube.Utils.AdvConsole;

namespace HyperCube.Models
{    
    public enum ArticleStatus {
        [Display(Name = "Новая")]
        New = 0,
        [Display(Name = "Сохранена")]
        Saved,
        [Display(Name = "Ожидает верификации")]
        AwatingVerify,
        [Display(Name = "Верифицируется")]
        Verifying,
        [Display(Name = "Верифицирована")]
        Verified,
        [Display(Name = "Отклонена")]
        Rejected,
        [Display(Name = "Удалена")]
        Deleted }

    public class ArticleModel : ICloneable
    {
        public int ID { get; set; }
        public string Filename { get; set; }
        public string HashSum { get; set; }
        public string FilenameReal { get { return ID + "_" + Filename; } }
        [Required]        
        public string Name { get; set; }        
        [Required]
        public DateTime PublishDate { get; set; } = DateTime.Now.Date;
        [Required]
        public string Authors { get; set; }
        [Required]
        public string Keywords { get; set; }
        [Required]
        public string Annotation { get; set; }
        public string Text { get; set; }        

        public ArticleStatus Status { get; set; } = ArticleStatus.New;

        public int? Rating
        {
            get { return rating; }
            set
            {
                rating = value;
                if (rating < 1) rating = 1;
                if (rating > 5) rating = 5;
            }
        }

        private int? rating;
        private string initiatorUUID = "";

        public static ArticleModel Find(int id)
        {
            if (id > 0) {

                if (AppData.Articles.ContainsKey(id))
                    return AppData.Articles[id];
            }
            return null;
        }

        public async Task<int> GetEditsCount(string acc_id = "")
        {
            MySQLConnector dbCon = MySQLConnector.Instance();

            string stringSQL = $"SELECT COUNT(*) " +
                $"FROM articles_edit_log ";
            if (acc_id.Length < 36)
                stringSQL += $"WHERE article_id={this.ID}";            
            else
                stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";

            int count = await dbCon.SQLSelectCount(stringSQL);
            //dbCon.Close();
            return count;
        }

        public async Task<string> GetInitiatorUUID()
        {
            if (initiatorUUID.Length < 36)
            {
                Console.WriteLine("initiatorUUID is null, getting it from DB");

                MySQLConnector dbCon = MySQLConnector.Instance();

                string stringSQL = $"SELECT acc_id " +
                    $"FROM articles " +
                    $"JOIN actions_history ON actions_history.article_id = articles.id " +
                    $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Saved}";

                initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
                //dbCon.Close();                
            }
            else
                Console.WriteLine("initiatorUUID already set");

            return initiatorUUID;
        }

        public object Clone()
        {
            return MemberwiseClone();
        }
    }
}
