using Ato.CD.Inbound.Shared;
using Ato.EN.IntegrationServices.CodeGenerationRPTTAXPOS;
using Ato.EN.IntegrationServices.CodeGenerationTRT;
using DataContracts;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Linq;

namespace Ato.CD.Inbound.TRT
{
    public class CrossFormValidatorRPTTAXPOS : ICrossFormValidator
    {
        private RPTTAXPOS2019 childDocument { get; set; }

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

        private TRT2026 parentDocument { get; set; }

        public CrossFormValidatorRPTTAXPOS(TRT2026 parent, object child)
        {
            childDocument = (RPTTAXPOS2019)child;
            parentDocument = parent;
            response = new List<ProcessMessageDocument>();
        }

        public List<ProcessMessageDocument> ValidateCrossFormRules()
        {
            //Parent form cross form rules
            VRATOTRT442917();
            VRATOTRT442918();
            VRATOTRT442919();
            VRATOTRT442920();

            return response;
        }

        #region VR.ATO.TRT.442917

            /*  VR.ATO.TRT.442917
            The first four digits of RTP category number must refer to your current financial year

            Technical Business Rule:
            CountDocument('RPTTAXPOS') = 1 AND Substring(^RPTTAXPOS4,1,4) <> ^TRT1

            Data Elements:

            ^TRT1 = TRT:RP:Report.TargetFinancial.Year

            ^RPTTAXPOS4 = RPTTAXPOS:RP:ReportableTaxPositionCategoryAAndB:RegulatoryDisclosures.ReportableTaxPositionCategoryReferenceNumber.Identifier
            */
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATOTRT442917()
        {
            if (childDocument.ReportableTaxPositionCategoryAAndBCollection != null)
                response.AddRange(from rptCategoryAAndB in childDocument.ReportableTaxPositionCategoryAAndBCollection
                where rptCategoryAAndB.RPTTAXPOS4 != null && rptCategoryAAndB.RPTTAXPOS4.Length >= 4 && rptCategoryAAndB.RPTTAXPOS4.Substring(0, 4).ToString() != parentDocument.TRT1.GetValueOrDefault().ToString()
                select new ProcessMessageDocument
                {
                    Code = "CMN.ATO.TRT.442917",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"The first four digits of RTP category number must refer to your current financial year",
                    Location = "/tns:TRT/tns:RP/tns:TargetFinancialY",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.TRT.442917" },
                        new ProcessMessageParameter { Name = "TRT1", Value = TRT2026Validator.GetValueOrEmpty(parentDocument.TRT1) },
                        new ProcessMessageParameter { Name = "RPTTAXPOS4", Value = rptCategoryAAndB.RPTTAXPOS4 }
                    },
                    DocumentSequence = "parent_doc"
                });
        }

        #endregion // VR.ATO.TRT.442917
	
		#region VR.ATO.TRT.442918

        /*  VR.ATO.TRT.442918
        The start date in reportable tax position schedule must be the same as the start date provided in its parent form

        Technical Business Rule:
        CountDocument('RPTTAXPOS') = 1 AND ^TRT573 <> ^RPTTAXPOS:RP:period.startDate

        Data Elements:

        ^TRT573 = TRT:RP:Period.Start.Date

        ^RPTTAXPOS Start Date = RPTTAXPOS Context Start Date
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATOTRT442918()
        {
            ProcessMessageDocument processMessage;
            bool assertion = !parentDocument.TRT573.Equals(childDocument.RPStartDate);

            if (assertion)
            {
                processMessage = new ProcessMessageDocument
                {
                    Code = "CMN.ATO.TRT.442918",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"The start date in reportable tax position schedule must be the same as the start date provided in its parent form",
                    Location = "/tns:TRT/tns:RP/tns:PeriodStartD",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.TRT.442918" },
                        new ProcessMessageParameter { Name = "TRT573", Value = TRT2026Validator.GetValueOrEmptyDate(parentDocument.TRT573) },
                        new ProcessMessageParameter { Name = "RPTTAXPOS Start Date", Value = TRT2026Validator.GetValueOrEmptyDate(childDocument.RPStartDate) }
                    },
                    DocumentSequence = "parent_doc"
                };

                response.Add(processMessage);
            }
        }

        #endregion // VR.ATO.TRT.442918
		
		#region VR.ATO.TRT.442919

        /* VR.ATO.TRT.442919
        The end date in reportable tax position schedule must be the same as the end date provided in its parent form

        Technical Business Rule:
        CountDocument('RPTTAXPOS') = 1 AND ^TRT574 <> ^RPTTAXPOS:RP:period.endDate

        Data Elements:

        ^TRT574 = TRT:RP:Period.End.Date

        ^RPTTAXPOS End Date = RPTTAXPOS Context End Date
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATOTRT442919()
        {
            ProcessMessageDocument processMessage;
            bool assertion = !parentDocument.TRT574.Equals(childDocument.RPEndDate);

            if (assertion)
            {
                processMessage = new ProcessMessageDocument
                {
                    Code = "CMN.ATO.TRT.442919",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"The end date in reportable tax position schedule must be the same as the end date provided in its parent form",
                    Location = "/tns:TRT/tns:RP/tns:PeriodEndD",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.TRT.442919" },
                        new ProcessMessageParameter { Name = "TRT574", Value = TRT2026Validator.GetValueOrEmptyDate(parentDocument.TRT574) },
                        new ProcessMessageParameter { Name = "RPTTAXPOS1", Value = TRT2026Validator.GetValueOrEmptyDate(childDocument.RPEndDate) }
                    },
                    DocumentSequence = "parent_doc"
                };

                response.Add(processMessage);
            }
        }

        #endregion // VR.ATO.TRT.442919
		
		#region VR.ATO.TRT.442920

        /*  VR.ATO.TRT.442920
        The Tax File Number (TFN) provided for the reportable tax position schedule must be the same as the TFN of the parent form

        Technical Business Rule:
        CountDocument('RPTTAXPOS') = 1 AND ^TRT5 <> RPTTAXPOS:^RP:TFN

        Data Elements:

        ^TRT5 = TRT:RP:Identifiers.TaxFileNumber.Identifier

        ^RPTTAXPOS TFN  = RPTTAXPOS:RP:entity.identifier.TFN
        */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public virtual void VRATOTRT442920()
        {
            ProcessMessageDocument processMessage;
            bool assertion = !parentDocument.TRT5.Equals(childDocument.RPIdentifierTFN);

            if (assertion)
            {
                processMessage = 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 = "/tns:IDS/tns:RP/tns:TaxFileNumberId",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.TRT.442920" },
                        new ProcessMessageParameter { Name = "TRT5", Value = parentDocument.TRT5 },
                        new ProcessMessageParameter { Name = "RPTTAXPOS TFN", Value = childDocument.RPIdentifierTFN }
                    },
                    DocumentSequence = "parent_doc"
                };
             
                response.Add(processMessage);
            }
        }

        #endregion // VR.ATO.TRT.442920
		
    }
}