﻿using DertInfo.Util.Validation;
using DertInfo.WEB.Models;
using DertInfo.WEB.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Web;

namespace DertInfo.WEB.ViewModels.JudgeMinder
{
    public class PVM_DanceScore : DertViewModelBase
    {
        public PVM_DanceScore()
        { 
        }

        public PVM_DanceScore(DanceScore danceScore)
        {
            this.ToView(danceScore);

            //Assign Validation
            this.MinValue = 0;
            this.MaxValue = danceScore.ScoreCategory.MaxMarks;
        }

        public int DanceScoreId { get; set; }
        public int DanceId { get; set; }
        public int ScoreCategoryId { get; set; }

        public int MinValue { get; set; }
        public int MaxValue { get; set; }
        
        [Required(ErrorMessage = "Enter A Score")]
        [DynamicRange("minval", "maxval", ErrorMessage = "Score can only be between {0} and {1}")]
        [DisplayName("Score")]
        public decimal MarkGiven { get; set; }

        [DisplayName("Comment")]
        public string CommentGiven { get; set; }

        //State Management
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public bool IsDeleted { get; set; }

        //Additional
        public string ScoreCategoryName { get; set; }

        private void ToView(DanceScore danceScore)
        {

            bool prefillForms = Convert.ToBoolean(ConfigurationManager.AppSettings["PreFillForms"].ToString());

            if (prefillForms)
            {
                Random random = new Random();
                this.MarkGiven = danceScore.MarkGiven != 0 ? danceScore.MarkGiven : random.Next(0,15);
                this.CommentGiven = danceScore.CommentGiven != "0" && danceScore.CommentGiven != string.Empty ? danceScore.CommentGiven : "Comment" + random.Next(0, 100).ToString();
            }
            else
            {
                this.MarkGiven = danceScore.MarkGiven;
                this.CommentGiven = danceScore.CommentGiven;
            }

            this.DanceScoreId = danceScore.Id;
            this.DanceId = danceScore.DanceId;
            this.ScoreCategoryId = danceScore.ScoreCategoryId;
            
            this.DateCreated = danceScore.DateCreated;
            this.DateModified = danceScore.DateModified;
            this.CreatedBy = danceScore.CreatedBy;
            this.ModifiedBy = danceScore.ModifiedBy;
            this.IsDeleted = danceScore.IsDeleted;

            this.ScoreCategoryName = danceScore.ScoreCategory.Name;
        }

        public DanceScore ToModel()
        {
            DanceScore danceScore = new DanceScore();

            danceScore.Id = this.DanceScoreId;
            danceScore.DanceId = this.DanceId;
            danceScore.ScoreCategoryId = this.ScoreCategoryId;
            danceScore.MarkGiven = this.MarkGiven;
            danceScore.CommentGiven = this.CommentGiven;

            danceScore.DateCreated = this.DateCreated;
            danceScore.DateModified = this.DateModified;
            danceScore.CreatedBy = this.CreatedBy;
            danceScore.ModifiedBy = this.ModifiedBy;
            danceScore.IsDeleted = this.IsDeleted;

            return danceScore;
        }
    }


}