using Ato.CD.Inbound.Shared;
using Ato.EN.IntegrationServices.CodeGenerationRS;
using Ato.EN.IntegrationServices.CodeGenerationTRTAMI;
using DataContracts;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Ato.CD.Inbound.TRTAMI202602
{
    public class CrossFormValidatorRS : ICrossFormValidator
    {
        private RS2018 ChildDocument { get; set; }

        public List<ProcessMessageDocument> Response { get; private set; }

        private TRTAMI2026 ParentDocument { get; set; }

        public CrossFormValidatorRS(TRTAMI2026 parent, object child)
        {
            ChildDocument = (RS2018)child;
            ParentDocument = parent;
            Response = new List<ProcessMessageDocument>();
        }

        public List<ProcessMessageDocument> ValidateCrossFormRules()
        {
            VRATOGEN402009();
            VRATORS000137();
            VRATORS000138();
            VRATORS000150();

            return Response;
        }

        #region Validation Rules

        #region VR.ATO.GEN.402009

        /*  VR.ATO.GEN.402009
        The Entity Identifier on the Schedule must equal the Entity Identifier on the Parent Return

        Legacy Rule Format:
        IF (RP:entity.identifier.TFN <> PARENT RETURN:RP:entity.identifier.TFN)
        RETURN VALIDATION MESSAGE
        ENDIF

        Data Elements:

        ^TRTAMI4 = TRTAMI:RP:Identifiers.TaxFileNumber.Identifier

        ^RS TFN = RS TFN
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATOGEN402009()
        {
            bool assertion = !ParentDocument.TRTAMI4.Equals(ChildDocument.RPIdentifierTFN);

            if (assertion)
            {
                Response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.GEN.402009",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Your supplied TFN does not match the TFN supplied on the form it was submitted with",
                    LongDescription = @"Your supplied TFN does not match the TFN supplied on the parent form it was submitted with",
                    Location = "/xbrli:xbrl/xbrli:context/xbrli:entity/xbrli:identifier",
                    Parameters = new ProcessMessageParameters {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.GEN.402009" },
                        new ProcessMessageParameter { Name = "TRTAMI4", Value = TRTAMI2026Validator.GetValueOrEmpty(ParentDocument.TRTAMI4) },
                        new ProcessMessageParameter { Name = "RS TFN", Value = TRTAMI2026Validator.GetValueOrEmpty(ChildDocument.RPIdentifierTFN) }
                      
                    }
                });
            }
        }

        #endregion

        #region VR.ATO.RS.000137

        /*  VR.ATO.RS.000137
        Number of weeks property rented must be equal to or less than 52

        Legacy Rule Format:
        IF (PARENT RETURN <> "IITR") AND ([RS25] > 52)
            RETURN VALIDATION MESSAGE
        ENDIF

        Data Elements:

        ^RS25 = RS:RP:RealEstateProperty.RentalWeeks.Count
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATORS000137()
        {
            bool assertion = ChildDocument.RS25.GetValueOrDefault() > 52;

            if (assertion)
            {
                Response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.RS.000137",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Number of weeks property rented must not exceed 52",
                    Location = "/xbrli:xbrl/tns:RealEstateProperty.RentalWeeks.Count",
                    Parameters = new ProcessMessageParameters {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.RS.000137" },
                        new ProcessMessageParameter { Name = "RS25", Value = TRTAMI2026Validator.GetValueOrEmpty(ChildDocument.RS25) }
                    }
                });
            }
        }

        #endregion

        #region VR.ATO.RS.000138

        /*  VR.ATO.RS.000138
        Number of weeks property was available for rent this year must be equal to or less than 52

        Legacy Rule Format:
        IF (PARENT RETURN <> "IITR") AND ([RS27] > 52)
            RETURN VALIDATION MESSAGE
        ENDIF

        Data Elements:

        ^RS27 = RS:RP:RealEstateProperty.RentalWeeksAvailable.Count
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATORS000138()
        {
            bool assertion = ChildDocument.RS27.GetValueOrDefault() > 52;

            if (assertion)
            {
                Response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.RS.000138",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Number of weeks property was available for rent this year cannot exceed 52 weeks",
                    Location = "/xbrli:xbrl/tns:RealEstateProperty.RentalWeeksAvailable.Count",
                    Parameters = new ProcessMessageParameters {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.RS.000138" },
                        new ProcessMessageParameter { Name = "RS27", Value = TRTAMI2026Validator.GetValueOrEmpty(ChildDocument.RS27) }
                    }
                });
            }
        }

        #endregion

        #region VR.ATO.RS.000150

        /*  VR.ATO.RS.000150
        The Entity name must be the same as the name on the main form

        Legacy Rule Format:
        IF (PARENT RETURN <> SET("IITR","TRTAMI") AND [RS14] <> PARENT RETURN(pyde.xx.xx:OrganisationNameDetails.OrganisationalName.Text (IN TUPLE(xbrli\organisationame2.xx.xx:OrganisationNameDetails) WHERE (pyde.xx.xx:OrganisationNameDetails.OrganisationalType.Code = "MN") AND (pyde.xx.xx:OrganisationNameDetails.Currency.Code = "C")))) OR (PARENT RETURN = SET("IITR","TRTAMI") AND [RS14] <> PARENT RETURN:RP:pyde.xx.xx:OrganisationNameDetails.OrganisationalName.Text)
            RETURN VALIDATION MESSAGE
        ENDIF

        Data Elements:

        ^TRTAMI10 = TRTAMI:RP:CurrentTrust:OrganisationNameDetails.OrganisationalName.Text

        ^RS14 = RS:RP:OrganisationNameDetails.OrganisationalName.Text
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATORS000150()
        {
            bool? assertion = !ChildDocument.RS14?.Trim().Equals(ParentDocument.TRTAMI10.Trim());
              
            if (assertion.HasValue)
            {
                if (assertion.Value)
                {
                    Response.Add(new ProcessMessageDocument
                    {
                        Code = "CMN.ATO.RS.000150",
                        Severity = ProcessMessageSeverity.Error,
                        Description = @"The Entity name must be the same as the name on the main form",
                        Location = "/xbrli:xbrl/tns:OrganisationNameDetails.OrganisationalName.Text",
                        Parameters = new ProcessMessageParameters {
                            new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.RS.000150" },
                            new ProcessMessageParameter { Name = "TRTAMI10", Value = TRTAMI2026Validator.GetValueOrEmpty(ParentDocument.TRTAMI10) },
                            new ProcessMessageParameter { Name = "RS14", Value = TRTAMI2026Validator.GetValueOrEmpty(ChildDocument.RS14) },
                        }
                    });
                }
            }
        }

        #endregion

        #endregion
    }
}
