using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;


namespace MvcApplication1.Models
{
    public class OrganizationNavigationViewModel
    {
        public IEnumerable<OrganizationNavigationData> OrganizationNavigationData { get; set; }
    }

    public class OrganizationUsersPartialViewModel
    {
        public IEnumerable<OrganizationUser> OrganizationUsers { get; set; }
    }

    //[ModelBinder(typeof(OrganizationModelBinder))]
    public class OrganizationProfileViewModel : OrganizationEditable
    {

        [UIHint("DropDownList")]
        //[DropDownList(DropDownListTargetProperty = "OrganizationcategoryId")]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "OrganizationCategory")]
        public IEnumerable<SelectListItem> OrganizationCategory { get; set; }
    }

    public class RegisterOrganizationViewModel : OrganizationEditable
    {
        //[Required]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "OrganizationInformation")]
        //public OrganizationEditable OrganizationEditable { get; set; }

        [UIHint("DropDownList")]
        //[DropDownList(DropDownListTargetProperty = "OrganizationcategoryId")]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "OrganizationCategory")]
        public IEnumerable<SelectListItem> OrganizationCategory { get; set; }

        //[Required]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "OrganizationCategory")]
        //[DropDownList("orgCategory", "Id", "Name")]
        //public OrganizationCategory OrganizationCategory { get; set; }

    }

    public class OrganizationNavigationData
    {
        public string Text { get; set; }
        public string ImageUrl { get; set; }
        public string NavigateUrl { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        public RouteValueDictionary routeValues { get; set; }
        public string ContenUrl { get; set; }
    }

    public class OrganizationsAdmined
    {
        public Guid UserId {get;set;}
        public Guid OrganizationId {get;set;}
        public string OrganizationName {get;set;}

    }
    public static class OrganizationNavigationDataBuilder
    {
        public static IEnumerable<OrganizationNavigationData> BuildOrganizationNavigationData(IEnumerable<OrganizationsAdmined> OrganizationsAdmined,string ActionName)
        {
            IList<OrganizationNavigationData> Navi=new List<OrganizationNavigationData>();   
            foreach (OrganizationsAdmined o in OrganizationsAdmined)
            {
                string NaviUrlPrefix = ActionName+"?OrganizationId=";
                OrganizationNavigationData Nd = new OrganizationNavigationData
                {
                    Text = o.OrganizationName,
                    //routeValues = new RouteValueDictionary  {{"OrganizationId", o.OrganizationId }},
                    //ActionName = "MyOrganizationUsersPartial",
                    //ControllerName = "MyOrganization",
                    ContenUrl = NaviUrlPrefix + o.OrganizationId.ToString()
                };
                Navi.Add(Nd);
            }
            return Navi;
        }
    }

    public class OrganizationUser : BaseEntity
    {
        private Guid organizationId;
        private Guid userId;
        private string userName;
        private string email;
        private string organizationRole;

        public Guid OrganizationId
        {
            get { return organizationId; }
            set { this.organizationId = value; }
        }

        public Guid UserId
        {
            get { return userId; }
            set { this.userId = value; }
        }
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Username")]
        public string UserName
        {
            get { return userName; }
            set { this.userName = value.Trim(); }
        }
        //[DisplayNameLocalized(typeof(Resources.Globalization), "EmailAddress")]
        public string Email
        {
            get { return email; }
            set { this.email = value.Trim(); }
        }
        //[DisplayNameLocalized(typeof(Resources.Globalization), "OrganizationRole")]
        public string OrganizationRole
        {
            get { return organizationRole; }
            set { this.organizationRole = value; }
        }

    }


    public abstract class BaseEntity 
    {
        public BaseEntity()
        {
            ID = Guid.NewGuid();
        }
        [ScaffoldColumn(false)]
        public virtual Guid ID
        { get; set; }
        [ScaffoldColumn(false)] 
        public virtual Guid Updateby
        { get; set; }
        [ScaffoldColumn(false)] 
        public virtual DateTime LastUpdate
        { get; set; }
        [ScaffoldColumn(false)] 
        public virtual int RowStatus
        { get; set; }
        [ScaffoldColumn(false)] 
        public virtual Byte[] RowVersion
        { get; set; }

    }

    

    [DisplayColumn("FullAddress")] 
    public class Address :BaseEntity 
    {

        private string city;

        private string country;

        private string street;

        private string zip;


        public Address()
        {
            
        }

        [Required]
        [StringLength(255)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Street")]
        public string Street
        {
            get { return street; }
            set { this.street = value; }
        }

        [Required]
        [StringLength(50)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "City")]
        public string City
        {
            get { return city; }
            set { this.city = value; }
        }

        [Required]
        [StringLength(50)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Country")]
        public string Country
        {
            get { return country; }
            set { this.country = value; }
        }


        [Required]
        [StringLength(10)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Zip")]
        public string Zip
        {
            get { return zip; }
            set { this.zip = value; }
        }

        [ScaffoldColumn(false)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "FullAddress")]
        public string FullAddress
        {
            get
            {
                return street + ", " + city + ", " + zip + ", " + country;
            }
        }

    }

    //[KnownType(typeof(OrganizationEditable))]
    //[KnownType(typeof(OrganizationProfile))]
    public class Organization : BaseEntity
    {
        private Address address;

        private Guid? auditby;

        private Decimal? depositbalance;

        private string duns;

        private string faxnumber;

        private bool isvalid;

        private DateTime? lastaudit;

        private string name;

        private Guid organizationcategoryId;

        private string phonenumber;

        private string url;

        private string validoptions;


        public Organization()
        {
        }


        [Required]
        [StringLength(50)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Name")]
        //[RegularExpression(@"^/\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))|((\%3C)|<)[^\n]+((\%3E)|>)/ix$", ErrorMessageResourceName = "SQLOrCSSInjectionDetected", ErrorMessageResourceType = typeof(Globalization))]
        //[RegularExpression(@"^/((\%3C)|<)((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>)/I$", ErrorMessageResourceName = "InvalidUserNameFormat", ErrorMessageResourceType = typeof(Globalization))]
        public string Name
        {
            get { return name; }
            set { this.name = value.Trim(); }
        }

        [Required]
        [DisplayFormat(NullDisplayText = "(null value)")]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "FullAddress")]
        public Address Address
        {
            get { return address; }
            set { this.address = value; }
        }

        [Required]
        [DataType(DataType.PhoneNumber)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Phonenumber")]
        [RegularExpression(@"^(([0-9]{1,3})[- .(]*([0-9a-zA-Z]{2,3})[- .)]*[0-9a-zA-Z]{3,4}[- .]*[0-9a-zA-Z]{4})+$",ErrorMessage ="Invalid Phone Or FaxNumber Format")]
        public string Phonenumber
        {
            get { return phonenumber; }
            set { this.phonenumber = value; }
        }

        [DataType(DataType.PhoneNumber)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Faxnumber")]
        [RegularExpression(@"^(([0-9]{1,3})[- .(]*([0-9a-zA-Z]{2,3})[- .)]*[0-9a-zA-Z]{3,4}[- .]*[0-9a-zA-Z]{4})+$", ErrorMessage = "Invalid Phone Or FaxNumber Format")]
        public string Faxnumber
        {
            get { return faxnumber; }
            set { this.faxnumber = value; }
        }

        [DataType(DataType.Url)]
        [StringLength(255)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Url")]
        [RegularExpression(@"^(http[s]?://|ftp://)?(www\.)?[a-zA-Z0-9-\.]+\.(com|cn|sg|org|net|mil|edu|ca|co.uk|com.au|gov)$",ErrorMessage ="Invalid Url Format")]
        public string Url
        {
            get { return url; }
            set { this.url = value; }
        }

        [RegularExpression(@"^\d{9}$",ErrorMessage ="InvalidDUNS")]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Duns")]
        public string Duns
        {
            get { return duns; }
            set { this.duns = value; }
        }

        [ScaffoldColumn(false)]
        public virtual Guid OrganizationcategoryId
        {
            get { return organizationcategoryId; }
            set { this.organizationcategoryId = value; }
        }


        [DataType(DataType.DateTime)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Lastaudit")]
        public virtual DateTime? Lastaudit
        {
            get { return lastaudit; }
            set { this.lastaudit = value; }
        }

        //[DisplayNameLocalized(typeof(Resources.Globalization), "Auditby")]
        [HiddenInput]
        public virtual Guid? Auditby
        {
            get { return auditby; }
            set { this.auditby = value; }
        }


        //[DisplayNameLocalized(typeof(Resources.Globalization), "Isvalid")]
        public virtual bool Isvalid
        {
            get { return isvalid; }
            set { this.isvalid = value; }
        }

        [DataType(DataType.MultilineText)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Validoptions")]
        public virtual string Validoptions
        {
            get { return validoptions; }
            set { this.validoptions = value; }
        }

    }

    public class OrganizationEditable : Organization
    {


        private Guid? auditby;

        private Decimal? depositbalance;

        private bool isvalid;

        private DateTime? lastaudit;

        private Guid organizationcategoryId;

        private string validoptions;





        public OrganizationEditable()
        {
        }


        [HiddenInput]
        public override Guid OrganizationcategoryId
        {
            get { return organizationcategoryId; }
            set { this.organizationcategoryId = value; }
        }

        [ScaffoldColumn(false)]
        [DataType(DataType.DateTime)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Lastaudit")]
        public override DateTime? Lastaudit
        {
            get { return lastaudit; }
            set { this.lastaudit = value; }
        }

        [ScaffoldColumn(false)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Auditby")]
        [HiddenInput]
        public override Guid? Auditby
        {
            get { return auditby; }
            set { this.auditby = value; }
        }


        [ScaffoldColumn(false)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Isvalid")]
        public override bool Isvalid
        {
            get { return isvalid; }
            set { this.isvalid = value; }
        }

        [ScaffoldColumn(false)]
        [DataType(DataType.MultilineText)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Validoptions")]
        public override string Validoptions
        {
            get { return validoptions; }
            set { this.validoptions = value; }
        }

    }

    

   public class OrganizationCategory :BaseEntity 
    {

        private string description;

        private bool isvalid;

        private string name;


        public OrganizationCategory()
        {
        }

        [Required]
        [StringLength(50)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Description")]
        public string Description
        {
            get { return description; }
            set { this.description = value; }
        }

        [Required]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Isvalid")]
        public bool Isvalid
        {
            get { return isvalid; }
            set { this.isvalid = value; }
        }

        [Required]
        [StringLength(18)]
        //[DisplayNameLocalized(typeof(Resources.Globalization), "Name")]
        public string Name
        {
            get { return name; }
            set { this.name = value; }
        }



    }
}




