using SEI2.TestConsole.SEI2ReportProxy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SEI2.TestConsole.Bundles
{
    public class BundleDataCreator
    {
        private string userGroup;

        public BundleDataCreator(string userGroup = "GRP-000014")
        {
            this.userGroup = userGroup;
        }

        public string ReportDataContract()
        {
            var contract = this.createDataContract();

            return this.Serialize(contract);
        }

        public string ReportDataContractWithError()
        {
            var contract = this.createDataContract("777777777777777");

            return this.Serialize(contract);
        }

        public string UpdateDataContract(Guid existingSchemaGuid)
        {
            var contract = this.createDataContract();
            contract.schemaGuid = existingSchemaGuid;

            return this.Serialize(contract);
        }

        public string CancelDataContract(Guid existingSchemaGuid)
        {
            SEI2SchemaCancelContract contract = new SEI2SchemaCancelContract()
            {
                schemaGuid = existingSchemaGuid,
                schemaType = SEI2SchemaType.BedPlaces
            };

            return this.Serialize(contract);
        }

        public string UserGetContract()
        {
            SEI2DataContractUserMgmtUserGet contract = new SEI2DataContractUserMgmtUserGet()
            {
                sei2UserId = "BRG-000000026", // "BRG-000000026"
                certificateId = "" //"UI:DK-O:G:543c364a-5227-440f-b28a-1c9984feb342"
            };

            return this.Serialize(contract);
        }

        private SEI2SchemaBedPlacesContract createDataContract(string sorCode = "100051000016007")
        {
            SEI2SchemaBedPlacesContract contract = new SEI2SchemaBedPlacesContract()
            {
                schemaCreatedDate = DateTime.Now,
                schemaGuid = Guid.Empty,
                schemaUserGroupId = userGroup
            };

            List<SEI2SchemaBedPlacesTransContract> transList = new List<SEI2SchemaBedPlacesTransContract>();
            SEI2SchemaBedPlacesTransContract trans = new SEI2SchemaBedPlacesTransContract()
            {
                normedPatientDate = DateTime.Now,
                avaliableBedPlaces = 10,
                normedBedPlaces = 10,
                numberOfPatients = 10,
                sORCode = sorCode
            };
            transList.Add(trans);

            contract.bedPlacesTrans = transList.ToArray();

            return contract;
        }

        private string Serialize(object obj)
        {
            if (obj == null)
                return string.Empty;

            using (MemoryStream stream = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
                serializer.WriteObject(stream, obj);
                stream.Seek(0, SeekOrigin.Begin);
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}
