public static async Task <int> SaveServiceVisit(ServiceVisit serviceVisit)
        {
            ServiceVisitDBInfo serviceVisitDBInfo = await App.ServiceVisitsDatabase.GetServiceVisitAsync(serviceVisit.ServiceVisitId);

            var result = await App.ServiceVisitsDatabase.SaveServiceVisitAsync(serviceVisitDBInfo);

            return(result);
        }
        public ServiceVisitViewModel(ServiceVisit visit)
        {
            //load lists of postcodes and suburbs
            List <Locale> locales = File.ReadAllLines("australian_postcodes.csv")
                                    .Skip(1)
                                    .Select(v => new Locale(v))
                                    .ToList();

            //TODO ServiceVisitNumber
            Id                  = visit.ServiceVisitId;
            DueDate             = visit.DueDate;
            ServiceVisitNumber  = visit.ServiceVisitId;
            SiteId              = visit.SiteId;
            SiteName            = visit.SiteName;
            SiteAddressLine1    = visit.Address.SiteAddressLine1;
            SiteAddressLine2    = visit.Address.SiteAddressLine2;
            SiteAddressLine3    = visit.Address.SiteAddressLine3;
            SiteAddressPostCode = visit.Address.SiteAddressPostCode;

            //search for suburb based on postcode
            Locale currentLocale = locales.Where(l => l.PostCode == visit.Address.SiteAddressPostCode).FirstOrDefault();

            if (currentLocale != null)
            {
                SiteSuburb = currentLocale.Suburb;
            }

            ContactMemo   = visit.Memos.ContactMemo;
            InductionMemo = visit.Memos.InductionMemo;
            OHSMemo       = visit.Memos.OHSMemo;
            FSMMemo       = visit.Memos.FSMMemo;
            ServiceMemo   = visit.Memos.ServiceMemo;

            ContactName      = visit.Contact.ContactName;
            ContactTelephone = visit.Contact.ContactTelephone;
            ContactMobile    = visit.Contact.ContactMobile;
            ContactEmail     = visit.Contact.ContactEmail;

            BuildingEra   = visit.Building.BuildingEra;
            BuildingSize  = visit.Building.BuildingSize;
            BuildingParts = visit.Building.BuildingParts;
            BuildingClass = visit.Building.BuildingClass;
            KeyHeld       = visit.Building.KeysHeld != 0;

            PositionLat    = visit.PositionLat;
            PostionLong    = visit.PositionLong;
            SiteImageUrl   = visit.SiteImageURL.Length > 0 ? visit.SiteImageURL : "no-image.png";
            ScheduledDate  = visit.ScheduledDate;
            TasksRemaining = visit.TasksRemaining;
            TotalTasks     = visit.TotalTasks;
            Status         = visit.Status;

            // for details page
            _buildingEraIndex   = _buildingDetails.GetBuildingDetailsEraIndex(BuildingEra);
            _buildingSizeIndex  = _buildingDetails.GetBuildingDetailsSizeIndex(BuildingSize);
            _buildingClassIndex = _buildingDetails.GetBuildingDetailsClassIndex(BuildingClass);
        }
        public static async Task <int> SaveServiceVisitReschedule(ServiceVisit serviceVisit, DateTime dateTime)
        {
            ServiceVisitDBInfo serviceVisitDBInfo = await App.ServiceVisitsDatabase.GetServiceVisitAsync(serviceVisit.ServiceVisitId);

            serviceVisitDBInfo.ScheduledDate    = dateTime;
            serviceVisitDBInfo.updateReschedule = true;
            var result = await App.ServiceVisitsDatabase.UpdateServiceVisitAsync(serviceVisitDBInfo);

            return(result);
        }
        /// <summary>
        /// This is used to update Memos
        /// </summary>
        /// <param name="SiteId">Unique Id for site, used to target ContactMemo,InductionMemo and OHCMemo</param>
        /// <param name="ServiceVisitNumber">Unique Id for service visit, used to target ServiceMemo</param>
        /// <param name="Memo">All the new Memos</param>
        /// <returns></returns>
        public async Task <bool> UpdateMemo(int SiteId, int ServiceVisitNumber, Memos Memo)
        {
            String url = Configuration.baseURL + "/site/" + SiteId + "/memos";
            String urlForServiceMemo = Configuration.baseURL + "/ServiceVisits/" + ServiceVisitNumber + "/servicememo";

            var json    = JsonConvert.SerializeObject(Memo);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var json2    = JsonConvert.SerializeObject(Memo);
            var content2 = new StringContent(json, Encoding.UTF8, "application/json");

            //set token
            String token = CrossSecureStorage.Current.GetValue("Token");

            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            var current = Connectivity.NetworkAccess;

            if (current == NetworkAccess.Internet)
            {
                var request1 = _httpClient.PutAsync(url, content);
                var request2 = _httpClient.PutAsync(urlForServiceMemo, content2);

                await Task.WhenAll(request1, request2);

                var respond1 = await request1;
                var respond2 = await request2;

                if (respond1.StatusCode == System.Net.HttpStatusCode.OK &&
                    respond2.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                var result = await ServiceVisitDBHelper.GetServiceVisit(ServiceVisitNumber);

                if (result != null)
                {
                    ServiceVisit serviceVisit = (ServiceVisit)(result);
                    await ServiceVisitDBHelper.SaveServiceVisitMemo(serviceVisit, Memo);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public ServiceVisit getServiceVisit()
        {
            ServiceVisit serviceVisit = new ServiceVisit();

            serviceVisit.ServiceVisitId = this.ServiceVisitId;
            serviceVisit.DueDate        = this.DueDate;
            serviceVisit.SiteId         = this.SiteId;
            serviceVisit.SiteName       = this.SiteName;

            Address address = new Address();

            address.SiteAddressLine1    = this.SiteAddressLine1;
            address.SiteAddressLine2    = this.SiteAddressLine2;
            address.SiteAddressLine3    = this.SiteAddressLine3;
            address.SiteAddressState    = this.SiteAddressState;
            address.SiteAddressPostCode = this.SiteAddressPostCode;
            serviceVisit.Address        = address;

            Memos memos = new Memos();

            memos.ContactMemo   = this.ContactMemo;
            memos.InductionMemo = this.InductionMemo;
            memos.OHSMemo       = this.OHSMemo;
            memos.FSMMemo       = this.FSMMemo;
            memos.ServiceMemo   = this.ServiceMemo;
            serviceVisit.Memos  = memos;

            Contact contact = new Contact();

            contact.ContactName      = this.ContactName;
            contact.ContactTelephone = this.ContactTelephone;
            contact.ContactMobile    = this.ContactMobile;
            contact.ContactEmail     = this.ContactEmail;
            serviceVisit.Contact     = contact;

            Building building = new Building();

            building.BuildingEra   = this.BuildingEra;
            building.BuildingSize  = this.BuildingSize;
            building.BuildingParts = this.BuildingParts;
            building.BuildingClass = this.BuildingClass;
            serviceVisit.Building  = building;


            serviceVisit.Building.KeysHeld = this.KeysHeld;
            serviceVisit.PositionLat       = this.PositionLat;
            serviceVisit.PositionLong      = this.PositionLong;
            serviceVisit.SiteImageURL      = this.SiteImageURL;
            serviceVisit.ScheduledDate     = this.ScheduledDate;
            serviceVisit.TasksRemaining    = this.TasksRemaining;
            serviceVisit.TotalTasks        = this.TotalTasks;
            serviceVisit.Status            = (ServiceVisitStatus)Enum.Parse(typeof(ServiceVisitStatus), this.serviceVisitStatus);

            return(serviceVisit);
        }
        public static async Task <int> SaveServiceVisitDeleteSchedule(ServiceVisit serviceVisit)
        {
            ServiceVisitDBInfo serviceVisitDBInfo = await App.ServiceVisitsDatabase.GetServiceVisitAsync(serviceVisit.ServiceVisitId);

            serviceVisitDBInfo.ScheduledDate  = null;
            serviceVisitDBInfo.deleteSchedule = true;
            Console.WriteLine("delete executed" + serviceVisitDBInfo.deleteSchedule);
            var result = await App.ServiceVisitsDatabase.UpdateServiceVisitAsync(serviceVisitDBInfo);

            return(result);
        }
        public static async Task <int> SaveServiceVisitBuilding(ServiceVisit serviceVisit, Building building)
        {
            ServiceVisitDBInfo serviceVisitDBInfo = await App.ServiceVisitsDatabase.GetServiceVisitAsync(serviceVisit.ServiceVisitId);

            serviceVisitDBInfo.BuildingClass         = building.BuildingClass;
            serviceVisitDBInfo.BuildingEra           = building.BuildingEra;
            serviceVisitDBInfo.BuildingParts         = building.BuildingParts;
            serviceVisitDBInfo.BuildingSize          = building.BuildingSize;
            serviceVisitDBInfo.updateBuildingDetails = true;
            var result = await App.ServiceVisitsDatabase.UpdateServiceVisitAsync(serviceVisitDBInfo);

            return(result);
        }
        public static async Task <int> SaveServiceVisitMemo(ServiceVisit serviceVisit, Memos memo)
        {
            ServiceVisitDBInfo serviceVisitDBInfo = await App.ServiceVisitsDatabase.GetServiceVisitAsync(serviceVisit.ServiceVisitId);

            serviceVisitDBInfo.ContactMemo   = memo.ContactMemo;
            serviceVisitDBInfo.FSMMemo       = memo.FSMMemo;
            serviceVisitDBInfo.InductionMemo = memo.InductionMemo;
            serviceVisitDBInfo.OHSMemo       = memo.OHSMemo;
            serviceVisitDBInfo.ServiceMemo   = memo.ServiceMemo;
            serviceVisitDBInfo.updateMemos   = true;
            var result = await App.ServiceVisitsDatabase.UpdateServiceVisitAsync(serviceVisitDBInfo);

            return(result);
        }
        /// <summary>
        /// This is used to update details of building
        /// </summary>
        /// <param name="SiteId">Unique id for each site</param>
        /// <param name="BuildingDetails">New Building details</param>
        /// <returns></returns>
        public async Task <bool> UpdateBuildingDetails(int SiteId, Building BuildingDetails)
        {
            String url     = Configuration.baseURL + "/site/" + SiteId + "/building";
            var    json    = JsonConvert.SerializeObject(BuildingDetails);
            var    content = new StringContent(json, Encoding.UTF8, "application/json");

            //set token
            String token = CrossSecureStorage.Current.GetValue("Token");

            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            var current = Connectivity.NetworkAccess;

            if (current == NetworkAccess.Internet)

            {
                var respond = await _httpClient.PutAsync(url, content);

                if (respond.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                var result = await ServiceVisitDBHelper.GetServiceVisitBySiteId(SiteId);

                if (result != null)
                {
                    ServiceVisit serviceVisit = (ServiceVisit)(result);
                    await ServiceVisitDBHelper.SaveServiceVisitBuilding(serviceVisit, BuildingDetails);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
        public ServiceVisitDBInfo(ServiceVisit serviceVisit)
        {
            this.ServiceVisitId = serviceVisit.ServiceVisitId;

            this.DueDate             = serviceVisit.DueDate;
            this.SiteId              = serviceVisit.SiteId;
            this.SiteName            = serviceVisit.SiteName;
            this.SiteAddressLine1    = serviceVisit.Address.SiteAddressLine1;
            this.SiteAddressLine2    = serviceVisit.Address.SiteAddressLine2;
            this.SiteAddressLine3    = serviceVisit.Address.SiteAddressLine3;
            this.SiteAddressState    = serviceVisit.Address.SiteAddressState;
            this.SiteAddressPostCode = serviceVisit.Address.SiteAddressPostCode;
            this.ContactMemo         = serviceVisit.Memos.ContactMemo;
            this.InductionMemo       = serviceVisit.Memos.InductionMemo;
            this.OHSMemo             = serviceVisit.Memos.OHSMemo;
            this.FSMMemo             = serviceVisit.Memos.FSMMemo;
            this.ServiceMemo         = serviceVisit.Memos.ServiceMemo;
            this.ContactName         = serviceVisit.Contact.ContactName;
            this.ContactTelephone    = serviceVisit.Contact.ContactTelephone;
            this.ContactMobile       = serviceVisit.Contact.ContactMobile;
            this.ContactEmail        = serviceVisit.Contact.ContactEmail;
            this.BuildingEra         = serviceVisit.Building.BuildingEra;
            this.BuildingSize        = serviceVisit.Building.BuildingSize;
            this.BuildingParts       = serviceVisit.Building.BuildingParts;
            this.BuildingClass       = serviceVisit.Building.BuildingClass;
            this.KeysHeld            = serviceVisit.Building.KeysHeld;
            this.PositionLat         = serviceVisit.PositionLat;
            this.PositionLong        = serviceVisit.PositionLong;
            this.SiteImageURL        = serviceVisit.SiteImageURL;
            this.ScheduledDate       = serviceVisit.ScheduledDate;
            this.TasksRemaining      = serviceVisit.TasksRemaining;
            this.TotalTasks          = serviceVisit.TotalTasks;
            this.serviceVisitStatus  = serviceVisit.Status.ToString();
            //this.ServiceVisitIdNum = serviceVisit.ServiceVisitId;
            //this.updateBuildingDetails = false;
            //this.updateMemos = false;
            //this.updateReschedule = false;
            //this.deleteSchedule = false;
        }
        public async void RescheduleServiceVisitSchedule(String ServiceVisitId, DateTime dateTime)
        {
            String url = Configuration.baseURL + "/ServiceVisits/" + ServiceVisitId + "/schedule";

            try
            {
                //set token
                String token = CrossSecureStorage.Current.GetValue("Token");
                _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

                var current = Connectivity.NetworkAccess;
                if (current == NetworkAccess.Internet)
                {
                    _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                    //JObject content = new JObject();

                    //content.Add("DateTime", "{\"DateTime\":\"" + String.Format("{0:s}", dateTime) + "\"}");
                    var content = new StringContent("{\"DateTime\":\"" + String.Format("{0:s}", dateTime) + "\"}", Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await _httpClient.PutAsync(url, content);

                    //convert json to model
                }
                else
                {
                    var result = await ServiceVisitDBHelper.GetServiceVisit(Int32.Parse(ServiceVisitId));

                    if (result != null)
                    {
                        ServiceVisit serviceVisit = (ServiceVisit)(result);
                        await ServiceVisitDBHelper.SaveServiceVisitReschedule((ServiceVisit)(serviceVisit), dateTime);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        public async Task <bool> DeleteServiceVisitSchedule(String ServiceVisitId)
        {
            String url = Configuration.baseURL + "/ServiceVisits/" + ServiceVisitId + "/schedule";

            try
            {
                //set token
                String token = CrossSecureStorage.Current.GetValue("Token");
                _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);


                var current = Connectivity.NetworkAccess;
                if (current == NetworkAccess.Internet)
                {
                    HttpResponseMessage response = await _httpClient.DeleteAsync(url);

                    return(true);
                }
                else
                {
                    var result = await ServiceVisitDBHelper.GetServiceVisit(Int32.Parse(ServiceVisitId));

                    if (result != null)
                    {
                        ServiceVisit serviceVisit = (ServiceVisit)(result);
                        await ServiceVisitDBHelper.SaveServiceVisitDeleteSchedule((ServiceVisit)(serviceVisit));
                    }
                    return(true);
                }

                //convert json to model
            }
            catch (Exception)
            {
                return(false);
            }
        }