//private MitchellClaimType readXml()
        //{
        //    XmlSerializer reader = new XmlSerializer(typeof(MitchellClaimType));
        //    System.IO.StreamReader file = new System.IO.StreamReader($"\\Assets\\create-claim.xml");
        //    MitchellClaimType MitchellClaim = (MitchellClaimType)reader.Deserialize(file);
        //    file.Close();

        //    return MitchellClaim;
        //}

        private MitchellClaimType CreateOne()
        {
            var claim = new MitchellClaimType()
            {
                Id = 1,
                ClaimNumber = "22c9c23bac142856018ce14a26b6c299",
                ClaimantFirstName = "George",
                ClaimantLastName = "Washington",
                Status = StatusCode.OPEN,
                LossDate = Convert.ToDateTime("2014-07-09T17:19:13.631-07:00"),
                AssignedAdjusterID = 12345,
            };

            claim.LossInfo = new LossInfoType()
            {
                Id = 1,
                CauseOfLoss = (CauseOfLossCode)Enum.Parse(typeof(CauseOfLossCode), "Collision"),
                ReportedDate = Convert.ToDateTime("2014-07-10T17:19:13.676-07:00"),
                LossDescription = "Crashed into an apple tree"
            };

            claim.Vehicles = new List<VehicleInfoType>()
            {
               new VehicleInfoType() {
                    Vin = "1M8GDM9AXKP042788",
                    ModelYear = 2015,
                    MakeDescription = "Ford",
                    ModelDescription = "Mustang",
                    EngineDescription = "EcoBoost",
                    ExteriorColor = "Deep Impact Blue",
                    LicPlate = "NO1PRES",
                    LicPlateState = "VA",
                    LicPlateExpDate = Convert.ToDateTime("2015-04-15T07:00"),
                    DamageDescription = "Front end smashed in. Apple dents in roof.",
                    Mileage = 1776
                }
            };

            return claim;
        }
        public bool AddNewClaim(MitchellClaimType claim)
        {
            try
            {
                if (_context.MitchellClaim.Any(c => c.ClaimNumber == claim.ClaimNumber) == false)
                {
                    foreach (var vehicle in claim.Vehicles)
                    {
                        _context.VehicleInfo.Add(vehicle);
                    }
                    _context.LossInfo.Add(claim.LossInfo);
                    _context.MitchellClaim.Add(claim);
                    return true;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Could not add new claim.", ex);
                return false;
            }

            return false;
        }
        public bool UpdateClaim(MitchellClaimType claimPassIn)
        {
            bool isChanged = false;

            //to test Xml file input
            // var claim = ReadUpdatefile();
            // var claim = GetModifiedClaim();

            var claim = claimPassIn;
            try
            {
                var claimToUpdate = _context.MitchellClaim.Include(c => c.Vehicles).Where(c => c.ClaimNumber == claim.ClaimNumber).SingleOrDefault();

                if (claimToUpdate != null)
                {
                    if ( claim.AssignedAdjusterID != 0 && claim.AssignedAdjusterID != claimToUpdate.AssignedAdjusterID)
                    {
                        claimToUpdate.AssignedAdjusterID = claim.AssignedAdjusterID;
                        isChanged = true;
                    }
                    //TODO  more fields to update

                    var vNew = claim.Vehicles.FirstOrDefault();
                    var vehicleToUpdate = claimToUpdate.Vehicles.Where(v => v.Vin == vNew.Vin).SingleOrDefault();
                    if (vehicleToUpdate != null)
                    {
                        if (vNew.ExteriorColor != null && vNew.ExteriorColor != "" && vNew.ExteriorColor != vehicleToUpdate.ExteriorColor)
                        {
                            vehicleToUpdate.ExteriorColor = vNew.ExteriorColor;
                            isChanged = true;
                        }

                        if (vNew.LicPlateExpDate != null && vNew.LicPlateExpDate != vehicleToUpdate.LicPlateExpDate)
                        {
                            vehicleToUpdate.LicPlateExpDate = vNew.LicPlateExpDate;
                            isChanged = true;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                _logger.LogError("claim update error", ex);
                return false;
            }

            return isChanged;
        }
       private MitchellClaimType GetModifiedClaim()
       {
           var claim = new MitchellClaimType()
           {
               ClaimNumber = "22c9c23bac142856018ce14a26b6c299",
               AssignedAdjusterID = 67890,
           };
 
           claim.Vehicles = new List<VehicleInfoType>()
           {
               new VehicleInfoType()
               {
                   Vin = "1M8GDM9AXKP042788",
                   ExteriorColor = "Competition Orange",
                   LicPlateExpDate = Convert.ToDateTime("2015-04-15T07:00")
               }
           };
           
           return claim;
       }