Crabtree/SmartHome/UI/SimpleControl/Phone/Register/HomeListView.cs
@@ -1,843 +1,871 @@
using System;
using System.Collections.Generic;
using service.hdlcontrol.com_push;
//using System;
//using System.Collections.Generic;
//using service.hdlcontrol.com_push;
namespace Shared.SimpleControl.Phone
{
    public class HomeListView : FrameLayout
    {
        public HomeListView ()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
        public void ShowHomeList ()
        {
            Application.RunOnMainThread (() => {
                MainPage.Loading.Start ("Please wait...");
            });
            System.Threading.Tasks.Task.Run (() => {
                loadHomeLists ();
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                    ShowHomeListView ();
                });
            });
        }
//namespace Shared.SimpleControl.Phone
//{
//    public class HomeListView : FrameLayout
//    {
//        public HomeListView ()
//        {
//            BackgroundColor = SkinStyle.Current.MainColor;
//        }
//        public void ShowHomeList ()
//        {
//            Application.RunOnMainThread (() => {
//                MainPage.Loading.Start ("Please wait...");
//            });
//            System.Threading.Tasks.Task.Run (() => {
//                loadHomeLists ();
//                Application.RunOnMainThread (() => {
//                    MainPage.Loading.Hide ();
//                    ShowHomeListView ();
//                });
//            });
//        }
        public void ShowHomeListView ()
        {
            RemoveAll ();
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
            };
            AddChidren (topView);
//        public void ShowHomeListView ()
//        {
//            RemoveAll ();
//            #region 标题
//            var topView = new FrameLayout () {
//                Y = Application.GetRealHeight (36),
//                Height = Application.GetRealHeight (90),
//            };
//            AddChidren (topView);
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                TextID = R.MyInternationalizationString.HouseList,
                TextSize = 19,
                TextColor = SkinStyle.Current.TextColor1
            };
            topView.AddChidren (title);
//            var title = new Button () {
//                TextAlignment = TextAlignment.Center,
//                TextID = R.MyInternationalizationString.HouseList,
//                TextSize = 19,
//                TextColor = SkinStyle.Current.TextColor1
//            };
//            topView.AddChidren (title);
            var logo = new Button () {
                Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                UnSelectedImagePath = MainPage.LogoString,
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren (logo);
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                this.RemoveFromParent ();
            };
            #endregion
//            var logo = new Button () {
//                Width = Application.GetRealWidth (154),
//                Height = Application.GetRealHeight (90),
//                X = Application.GetRealWidth (486),
//                UnSelectedImagePath = MainPage.LogoString,
//                Gravity = Gravity.CenterVertical,
//            };
//            topView.AddChidren (logo);
//            var back = new Button () {
//                Height = Application.GetRealHeight (90),
//                Width = Application.GetRealWidth (85),
//                UnSelectedImagePath = "Item/Back.png",
//                Gravity = Gravity.CenterVertical,
//            };
//            topView.AddChidren (back);
//            back.MouseUpEventHandler += (sender, e) => {
//                this.RemoveFromParent ();
//            };
//            #endregion
            var BodyView = new FrameLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (1136 - 126 - 90),
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (BodyView);
//            var BodyView = new FrameLayout () {
//                Width = LayoutParams.MatchParent,
//                Height = Application.GetRealHeight (1136 - 126 - 90),
//                Y = topView.Bottom,
//                BackgroundColor = SkinStyle.Current.ViewColor,
//            };
//            AddChidren (BodyView);
            VerticalScrolViewLayout VerticalScrolViewMiddle = new VerticalScrolViewLayout ();
            BodyView.AddChidren (VerticalScrolViewMiddle);
//            VerticalScrolViewLayout VerticalScrolViewMiddle = new VerticalScrolViewLayout ();
//            BodyView.AddChidren (VerticalScrolViewMiddle);
            System.Threading.Tasks.Task.Run (() => {
                try {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Start ("Please wait...");
                    });
//            System.Threading.Tasks.Task.Run (() => {
//                try {
//                    Application.RunOnMainThread (() => {
//                        MainPage.Loading.Start ("Please wait...");
//                    });
                    foreach (var home in UserConfig.Instance.HomeLists) {
                        List<GatewayRes> gatewayList = null;
                        //var requestObj3 = new GatewayByRegionListObj () { RegionID = home.Id };
                        //var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
                        var requestObj3 = new GetSingleHomeGatewayPaggerObj ();
                        requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
                        requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id;
                        requestObj3.ReqDto.PageSetting.Page = 1;
                        requestObj3.ReqDto.PageSetting.PageSize = 999;
//                    foreach (var home in UserConfig.Instance.HomeLists) {
//                        List<GatewayRes> gatewayList = null;
//                        //var requestObj3 = new GatewayByRegionListObj () { RegionID = home.Id };
//                        //var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
//                        var requestObj3 = new GetSingleHomeGatewayPaggerObj ();
//                        requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
//                        requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id;
//                        requestObj3.ReqDto.PageSetting.Page = 1;
//                        requestObj3.ReqDto.PageSetting.PageSize = 999;
//                        string urlHead = MainPage.RequestHttpsHost;
//                        if (requestObj3.IsOtherAccountCtrl) {
//                            urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
//                            requestObj3.ReqDto.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
//                        }
                        //var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
                        var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
                        var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3);
                        if (revertObj3.StateCode.ToUpper() == "SUCCESS") {
                            var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
                            gatewayList = infoResult.PageData;
                            if (gatewayList != null) {
                                Application.RunOnMainThread (() => {
                                    addRow (home, VerticalScrolViewMiddle, gatewayList.Count == 0 ? null : gatewayList [0]);
                                });
                            }
                        }
                    }
                } catch {
                    Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
            Button btnAddArea = new Button () {
                Y = Application.GetRealHeight (1136 - 93),
                Height = Application.GetRealHeight (93),
                TextColor = SkinStyle.Current.TextColor1,
                BackgroundColor = SkinStyle.Current.MainColor,
                TextID = R.MyInternationalizationString.IncreaseTheArea,
                TextAlignment = TextAlignment.Center
            };
            AddChidren (btnAddArea);
//                        //var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
//                        var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
//                        var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
//                        if (revertObj3.StateCode.ToUpper() == "SUCCESS") {
//                            var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
//                            gatewayList = infoResult.PageData;
//                            if (gatewayList != null) {
//                                Application.RunOnMainThread (() => {
//                                    addRow (home, VerticalScrolViewMiddle, gatewayList.Count == 0 ? null : gatewayList [0]);
//                                });
//                            }
//                        }
//                    }
//                } catch {
//                    Application.RunOnMainThread (() => {
//                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
//                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                    });
//                } finally {
//                    Application.RunOnMainThread (() => {
//                        MainPage.Loading.Hide ();
//                    });
//                }
//            });
            btnAddArea.MouseUpEventHandler += (sender1, e1) => {
                AddHomeDialog ();
            };
//            Button btnAddArea = new Button () {
//                Y = Application.GetRealHeight (1136 - 93),
//                Height = Application.GetRealHeight (93),
//                TextColor = SkinStyle.Current.TextColor1,
//                BackgroundColor = SkinStyle.Current.MainColor,
//                TextID = R.MyInternationalizationString.IncreaseTheArea,
//                TextAlignment = TextAlignment.Center
//            };
//            AddChidren (btnAddArea);
        }
//            btnAddArea.MouseUpEventHandler += (sender1, e1) => {
//                AddHomeDialog ();
//            };
        void addRow (RegionInfoRes home, VerticalScrolViewLayout VerticalScrolViewMiddle, GatewayRes gatewayObj)
        {
            string gatewayMAC = "";
            if (gatewayObj != null) {
                gatewayMAC = gatewayObj.GatewayUniqueId;
            }
            var rowView = new RowLayout () {
                Height = Application.GetRealHeight (110),
            };
            VerticalScrolViewMiddle.AddChidren (rowView);
//        }
            Button btnRowLine = new Button () {
                Y = Application.GetRealHeight (107),
                Height = Application.GetRealHeight (3),
                BackgroundColor = SkinStyle.Current.Black50Transparent
            };
            rowView.AddChidren (btnRowLine);
//        void addRow (RegionInfoRes home, VerticalScrolViewLayout VerticalScrolViewMiddle, GatewayRes gatewayObj)
//        {
//            string gatewayMAC = "";
//            if (gatewayObj != null) {
//                gatewayMAC = gatewayObj.GatewayUniqueId;
//            }
//            var rowView = new RowLayout () {
//                Height = Application.GetRealHeight (110),
//            };
//            VerticalScrolViewMiddle.AddChidren (rowView);
            var btnPoint = new Button () {
                Width = Application.GetRealWidth (10),
                Height = Application.GetRealHeight (10),
                X = Application.GetRealWidth (50),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Point.png",
                SelectedImagePath = "Item/Point.png",
                Visible = home.Id != UserConfig.Instance.CurrentRegion.Id
            };
            rowView.AddChidren (btnPoint);
//            Button btnRowLine = new Button () {
//                Y = Application.GetRealHeight (107),
//                Height = Application.GetRealHeight (3),
//                BackgroundColor = SkinStyle.Current.Black50Transparent
//            };
//            rowView.AddChidren (btnRowLine);
            var btnName = new Button () {
                Width = Application.GetRealWidth (350),
                Text = home.Name,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (90),
                TextColor = SkinStyle.Current.TextColor1,
            };
            rowView.AddChidren (btnName);
//            var btnPoint = new Button () {
//                Width = Application.GetRealWidth (10),
//                Height = Application.GetRealHeight (10),
//                X = Application.GetRealWidth (50),
//                Gravity = Gravity.CenterVertical,
//                UnSelectedImagePath = "Item/Point.png",
//                SelectedImagePath = "Item/Point.png",
//                Visible = home.Id != UserConfig.Instance.CurrentRegion.Id
//            };
//            rowView.AddChidren (btnPoint);
            btnName.MouseUpEventHandler += (ss, ee) => {
                AddHomeDialog (home, gatewayObj);
            };
//            var btnName = new Button () {
//                Width = Application.GetRealWidth (350),
//                Text = home.Name,
//                TextAlignment = TextAlignment.CenterLeft,
//                X = Application.GetRealWidth (90),
//                TextColor = SkinStyle.Current.TextColor1,
//            };
//            rowView.AddChidren (btnName);
            Button rightButton1 = new Button () {
                X = Application.GetRealWidth (550),
                Width = Application.GetRealWidth (28),
                Height = Application.GetRealHeight (40),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Right.png",
                SelectedImagePath = "Item/RightSelected.png",
            };
            rowView.AddChidren (rightButton1);
            EventHandler<MouseEventArgs> eHandler = (sender, e) => {
                if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
                    //UserConfig.Instance.GatewayMAC = gatewayMAC;
                    goSearchView ();
                } else {
                    Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchResidentialToGoInDeviceList), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                    alert.ResultEventHandler += (sender2, e2) => {
                        if (e2) {
                            ChangeRegion (home, gatewayMAC, true);
                        }
                    };
                    alert.Show ();
                }
            };
            rowView.MouseUpEventHandler += eHandler;
            rightButton1.MouseUpEventHandler += eHandler;
//            btnName.MouseUpEventHandler += (ss, ee) => {
//                AddHomeDialog (home, gatewayObj);
//            };
            if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
                Button btnCheck = new Button () {
                    X = Application.GetRealWidth (20),
                    Y = Application.GetRealHeight (25),
                    Width = Application.GetRealWidth (72),
                    Height = Application.GetRealHeight (61),
                    UnSelectedImagePath = "Skin/Check.png",
                };
                rowView.AddChidren (btnCheck);
            }
            if (UserConfig.Instance.CurrentRegion.Id != home.Id && MainPage.LoginUser.AccountType != 1) {
                Button btnDel = new Button () {
                    TextID = R.MyInternationalizationString.Del,
                    BackgroundColor = SkinStyle.Current.DelColor,
                };
                rowView.AddRightView (btnDel);
                btnDel.MouseUpEventHandler += (sd, ee) => {
                    Dialog dialog = new Dialog ();
//            Button rightButton1 = new Button () {
//                X = Application.GetRealWidth (550),
//                Width = Application.GetRealWidth (28),
//                Height = Application.GetRealHeight (40),
//                Gravity = Gravity.CenterVertical,
//                UnSelectedImagePath = "Item/Right.png",
//                SelectedImagePath = "Item/RightSelected.png",
//            };
//            rowView.AddChidren (rightButton1);
//            EventHandler<MouseEventArgs> eHandler = (sender, e) => {
//                if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
//                    //UserConfig.Instance.GatewayMAC = gatewayMAC;
//                    goSearchView ();
//                } else {
//                    Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchResidentialToGoInDeviceList), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
//                    alert.ResultEventHandler += (sender2, e2) => {
//                        if (e2) {
//                            ChangeRegion (home, gatewayMAC, true);
//                        }
//                    };
//                    alert.Show ();
//                }
//            };
//            rowView.MouseUpEventHandler += eHandler;
//            rightButton1.MouseUpEventHandler += eHandler;
                    FrameLayout dialogBody = new FrameLayout () {
                        Width = Application.GetRealWidth (500),
                        Height = Application.GetRealHeight (400),
                        Radius = 5,
                        BorderColor = SkinStyle.Current.Transparent,
                        BorderWidth = 0,
                        BackgroundColor = SkinStyle.Current.DialogColor,
                        Gravity = Gravity.Center,
                    };
                    dialog.AddChidren (dialogBody);
//            if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
//                Button btnCheck = new Button () {
//                    X = Application.GetRealWidth (20),
//                    Y = Application.GetRealHeight (25),
//                    Width = Application.GetRealWidth (72),
//                    Height = Application.GetRealHeight (61),
//                    UnSelectedImagePath = "Skin/Check.png",
//                };
//                rowView.AddChidren (btnCheck);
//            }
//            if (UserConfig.Instance.CurrentRegion.Id != home.Id && MainPage.LoginUser.AccountType != 1) {
//                Button btnDel = new Button () {
//                    TextID = R.MyInternationalizationString.Del,
//                    BackgroundColor = SkinStyle.Current.DelColor,
//                };
//                rowView.AddRightView (btnDel);
//                btnDel.MouseUpEventHandler += (sd, ee) => {
//                    Dialog dialog = new Dialog ();
                    Button btnTitle = new Button () {
                        Height = Application.GetRealHeight (80),
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                        TextAlignment = TextAlignment.Center,
                        TextID = R.MyInternationalizationString.DeleteTheArea,
                        TextColor = SkinStyle.Current.DialogTextColor,
                    };
                    dialogBody.AddChidren (btnTitle);
                    TextView btnTip = new TextView () {
                        X = Application.GetRealWidth (50),
                        Y = btnTitle.Bottom,
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (120),
                        TextColor = SkinStyle.Current.TextColor,
                        TextID = R.MyInternationalizationString.TipDelArea,
                        Enable = false,
                        TextSize = 14,
                        IsMoreLines = true,
                    };
                    dialogBody.AddChidren (btnTip);
                    btnTip.IsMoreLines = true;
                    EditText etPw = new EditText () {
                        X = Application.GetRealWidth (50),
                        Y = btnTip.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (70),
                        TextColor = SkinStyle.Current.TextColor,
                        Radius = 5,
                        BorderColor = SkinStyle.Current.BorderColor,
                        BorderWidth = 1,
                        SecureTextEntry = true,
                    };
                    dialogBody.AddChidren (etPw);
//                    FrameLayout dialogBody = new FrameLayout () {
//                        Width = Application.GetRealWidth (500),
//                        Height = Application.GetRealHeight (400),
//                        Radius = 5,
//                        BorderColor = SkinStyle.Current.Transparent,
//                        BorderWidth = 0,
//                        BackgroundColor = SkinStyle.Current.DialogColor,
//                        Gravity = Gravity.Center,
//                    };
//                    dialog.AddChidren (dialogBody);
                    FrameLayout bottomView = new FrameLayout () {
                        Y = Application.GetRealHeight (400 - 88),
                        Height = Application.GetRealHeight (90),
                        BackgroundColor = SkinStyle.Current.Black50Transparent,
                    };
                    dialogBody.AddChidren (bottomView);
//                    Button btnTitle = new Button () {
//                        Height = Application.GetRealHeight (80),
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                        TextAlignment = TextAlignment.Center,
//                        TextID = R.MyInternationalizationString.DeleteTheArea,
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                    };
//                    dialogBody.AddChidren (btnTitle);
//                    TextView btnTip = new TextView () {
//                        X = Application.GetRealWidth (50),
//                        Y = btnTitle.Bottom,
//                        Width = Application.GetRealWidth (400),
//                        Height = Application.GetRealHeight (120),
//                        TextColor = SkinStyle.Current.TextColor,
//                        TextID = R.MyInternationalizationString.TipDelArea,
//                        Enable = false,
//                        TextSize = 14,
//                        IsMoreLines = true,
//                    };
//                    dialogBody.AddChidren (btnTip);
//                    btnTip.IsMoreLines = true;
//                    EditText etPw = new EditText () {
//                        X = Application.GetRealWidth (50),
//                        Y = btnTip.Bottom + Application.GetRealHeight (10),
//                        Width = Application.GetRealWidth (400),
//                        Height = Application.GetRealHeight (70),
//                        TextColor = SkinStyle.Current.TextColor,
//                        Radius = 5,
//                        BorderColor = SkinStyle.Current.BorderColor,
//                        BorderWidth = 1,
//                        SecureTextEntry = true,
//                    };
//                    dialogBody.AddChidren (etPw);
                    Button btnClose = new Button () {
                        Width = Application.GetRealWidth (250),
                        TextAlignment = TextAlignment.Center,
                        TextColor = SkinStyle.Current.DialogTextColor,
                        TextID = R.MyInternationalizationString.Close,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                    };
                    bottomView.AddChidren (btnClose);
                    btnClose.MouseUpEventHandler += (asdf, eell) => {
                        dialog.Close ();
                    };
                    Button btnConfirm = new Button () {
                        X = Application.GetRealWidth (252),
                        Width = Application.GetRealWidth (250),
                        TextColor = SkinStyle.Current.DialogTextColor,
                        TextID = R.MyInternationalizationString.confirm,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                        TextAlignment = TextAlignment.Center,
                    };
                    bottomView.AddChidren (btnConfirm);
                    btnConfirm.MouseUpEventHandler += (dou, ni) => {
                        string password = etPw.Text.Trim ();
                        if (string.IsNullOrEmpty (password)) {
                            return;
                        }
                        System.Threading.Tasks.Task.Run (() => {
                            try {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Start ("Please wait...");
                                });
                                var requestObj = new LoginObj () { Account = MainPage.LoginUser.AccountString, Password = password };
                                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                                var revertObj = MainPage.RequestHttps (API.Login, requestJson, false, false);
                                if (revertObj.StateCode.ToUpper() == "SUCCESS") {
                                    var requestObj2 = new DelHomeObj () { HomeId = home.Id };
                                    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
                                    var revertObj2 = MainPage.RequestHttps (API.DelHome, requestJson2);
                                    if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
                                        Application.RunOnMainThread (() => {
                                            //new Alert ("", Language.StringByID (R.MyInternationalizationString.DeleteSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                            ShowHomeList ();
                                        });
                                    } else {
                                        ShowDelHomeErrorInfo (revertObj2.StateCode);
                                    }
                                }
                            } catch {
                                MainPage.FailureToServer ();
                            } finally {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Hide ();
                                    dialog.Close ();
                                });
                            }
                        });
                    };
//                    FrameLayout bottomView = new FrameLayout () {
//                        Y = Application.GetRealHeight (400 - 88),
//                        Height = Application.GetRealHeight (90),
//                        BackgroundColor = SkinStyle.Current.Black50Transparent,
//                    };
//                    dialogBody.AddChidren (bottomView);
                    dialog.Show ();
                };
            }
        }
//                    Button btnClose = new Button () {
//                        Width = Application.GetRealWidth (250),
//                        TextAlignment = TextAlignment.Center,
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                        TextID = R.MyInternationalizationString.Close,
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                    };
//                    bottomView.AddChidren (btnClose);
//                    btnClose.MouseUpEventHandler += (asdf, eell) => {
//                        dialog.Close ();
//                    };
//                    Button btnConfirm = new Button () {
//                        X = Application.GetRealWidth (252),
//                        Width = Application.GetRealWidth (250),
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                        TextID = R.MyInternationalizationString.confirm,
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                        TextAlignment = TextAlignment.Center,
//                    };
//                    bottomView.AddChidren (btnConfirm);
//                    btnConfirm.MouseUpEventHandler += (dou, ni) => {
//                        string password = etPw.Text.Trim ();
//                        if (string.IsNullOrEmpty (password)) {
//                            return;
//                        }
//                        System.Threading.Tasks.Task.Run (() => {
//                            try {
//                                Application.RunOnMainThread (() => {
//                                    MainPage.Loading.Start ("Please wait...");
//                                });
//                                var requestObj = new LoginObj () { Account = MainPage.LoginUser.AccountString, Password = password };
//                                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
//                                var revertObj = MainPage.RequestHttps (API.Login, requestJson);
//                                if (revertObj.StateCode.ToUpper() == "SUCCESS") {
//                                    var requestObj2 = new DelHomeObj () {
//                                        HomeId = home.Id ,
//                                        LoginAccessToken = MainPage.LoginUser.LoginTokenString
//                                };
//                                    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
//                                    var revertObj2 = MainPage.RequestHttps (API.DelHome, requestJson2);
//                                    if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
//                                        Application.RunOnMainThread (() => {
//                                            //new Alert ("", Language.StringByID (R.MyInternationalizationString.DeleteSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                                            ShowHomeList ();
//                                        });
//                                    } else {
//                                        ShowDelHomeErrorInfo (revertObj2.StateCode);
//                                    }
//                                }
//                            } catch {
//                                MainPage.FailureToServer ();
//                            } finally {
//                                Application.RunOnMainThread (() => {
//                                    MainPage.Loading.Hide ();
//                                    dialog.Close ();
//                                });
//                            }
//                        });
//                    };
        void ShowDelHomeErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == "NoLogin") {
                mes = ErrorCode.NoLogin;
            } else if (stateCodeStr == "CurrentHomeExixtsGatewayBind") {
                mes = ErrorCode.HomeIdAndTokenNoConsistent;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                //RequestServerFailed
                //mes = ErrorCode.AddFailed;
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
//                    dialog.Show ();
//                };
//            }
//        }
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
        }
//        void ShowDelHomeErrorInfo (string stateCodeStr)
//        {
//            string mes = "";
//            if (stateCodeStr == "NoLogin") {
//                mes = ErrorCode.NoLogin;
//            } else if (stateCodeStr == "CurrentHomeExixtsGatewayBind") {
//                mes = ErrorCode.HomeIdAndTokenNoConsistent;
//            } else if (stateCodeStr == ErrorCode.NetworkError) {
//                mes = ErrorCode.NetworkError;
//            } else {
//                //RequestServerFailed
//                //mes = ErrorCode.AddFailed;
//                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
        /// <summary>
        /// 进入后台搜索设备界面
        /// </summary>
        void goSearchView ()
        {
            var systemHomePage = new SystemHomePage ();
            UserMiddle.SettingPageView.AddChidren (systemHomePage);
            systemHomePage.Init ();
            UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 1;
        }
//            }
//            if (!string.IsNullOrEmpty (mes)) {
//                Application.RunOnMainThread (() => {
//                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                });
//            }
//        }
        /// <summary>
        /// 更换本地区域
        /// </summary>
        /// <param name="home">Home.</param>
        /// <param name="gatewayMAC">Gateway mac.</param>
        void ChangeRegion (RegionInfoRes home, string gatewayMAC, bool goSystem = false)
        {
            MainPage.Loading.Start ("Please wait...");
            System.Threading.Tasks.Task.Run (() => {
                try {
                    var backuplist = IO.FileUtils.ReadFiles ();
                    int index = 0;
                    if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Name)) {
                        UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0];
                    }
                    string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.Name);
                    IO.FileUtils.DeleteRegionFiles (oldRegionRootPath);
                    foreach (var fileName in backuplist) {
                        System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName);
                        if (fileInfo.Exists) {
                            fileInfo.MoveTo (oldRegionRootPath + fileName);
                            Console.WriteLine ("move file : " + fileName);
                        }
                        index++;
                        Application.RunOnMainThread (() => {
                            int pro = (int)(index * 1.0 / backuplist.Count * 50);
                            MainPage.Loading.Text = pro.ToString () + "%";
                        });
                    }
                    IO.FileUtils.DeleteAllFile ();
                    string newRegionRootPath = IO.FileUtils.CreateRegionBackup (home.Name);
                    IO.FileUtils.RestoreRegionFiles (newRegionRootPath);
                    UserConfig.Instance.CurrentRegion = home;
                    //UserConfig.Instance.GatewayMAC = gatewayMAC;
                    UserConfig.Instance.SaveUserConfig ();
                    Room.InitAllRoom ();
                    Application.RunOnMainThread (() => {
                        UserConfig.Instance.RefreshUserConfig ();
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully),
                                  Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        //this.RemoveFromParent ();
                        ShowHomeList ();
                        CommonPage.IsRemote = false;
                        EquipmentPublicClass.CheckLinkRemote (2);
                    });
                    MainPage.LoginUser.SaveUserInfo ();
                } catch (Exception ex) {
                    Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                    Console.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        if (goSystem)
                            goSearchView ();
                    });
                }
            });
        }
//        /// <summary>
//        /// 进入后台搜索设备界面
//        /// </summary>
//        void goSearchView ()
//        {
//            var systemHomePage = new SystemHomePage ();
//            UserMiddle.SettingPageView.AddChidren (systemHomePage);
//            systemHomePage.Init ();
//            UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 1;
//        }
//        /// <summary>
//        /// 更换本地区域
//        /// </summary>
//        /// <param name="home">Home.</param>
//        /// <param name="gatewayMAC">Gateway mac.</param>
//        void ChangeRegion (RegionInfoRes home, string gatewayMAC, bool goSystem = false)
//        {
//            MainPage.Loading.Start ("Please wait...");
//            System.Threading.Tasks.Task.Run (() => {
//                try {
//                    var backuplist = IO.FileUtils.ReadFiles ();
//                    int index = 0;
//                    if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Name)) {
//                        UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0];
//                    }
//                    string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.Name);
//                    IO.FileUtils.DeleteRegionFiles (oldRegionRootPath);
//                    foreach (var fileName in backuplist) {
//                        System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName);
//                        if (fileInfo.Exists) {
//                            fileInfo.MoveTo (oldRegionRootPath + fileName);
//                            Console.WriteLine ("move file : " + fileName);
//                        }
//                        index++;
//                        Application.RunOnMainThread (() => {
//                            int pro = (int)(index * 1.0 / backuplist.Count * 50);
//                            MainPage.Loading.Text = pro.ToString () + "%";
//                        });
//                    }
//                    IO.FileUtils.DeleteAllFile ();
//                    string newRegionRootPath = IO.FileUtils.CreateRegionBackup (home.Id);
//                    IO.FileUtils.RestoreRegionFiles (newRegionRootPath);
//                    UserConfig.Instance.CurrentRegion = home;
//                    //UserConfig.Instance.GatewayMAC = gatewayMAC;
//                    UserConfig.Instance.SaveUserConfig ();
//                    Room.InitAllRoom ();
//                    Application.RunOnMainThread (() => {
//                        UserConfig.Instance.RefreshUserConfig ();
//                        new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully),
//                                  Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                        //this.RemoveFromParent ();
//                        ShowHomeList ();
//                        CommonPage.IsRemote = false;
//                        EquipmentPublicClass.CheckLinkRemote (2);
//                    });
//                    MainPage.LoginUser.SaveUserInfo ();
//                } catch (Exception ex) {
//                    Application.RunOnMainThread (() => {
//                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
//                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                    });
//                    Console.WriteLine (ex.Message);
//                } finally {
//                    Application.RunOnMainThread (() => {
//                        MainPage.Loading.Hide ();
//                        if (goSystem)
//                            goSearchView ();
//                    });
//                }
//            });
//        }
        void AddHomeDialog (RegionInfoRes home = null, GatewayRes gatewayObj = null)
        {
            string gatewayMAC = "";
            if (gatewayObj != null) {
                gatewayMAC = gatewayObj.GatewayUniqueId;
            }
//        void AddHomeDialog (RegionInfoRes home = null, GatewayRes gatewayObj = null)
//        {
//            string gatewayMAC = "";
//            if (gatewayObj != null) {
//                gatewayMAC = gatewayObj.GatewayUniqueId;
//            }
            // 是否是修改已经存在的住宅
            bool isModify = home == null ? false : true;
            if (!isModify) {
                home = new RegionInfoRes ();
            }
//            // 是否是修改已经存在的住宅
//            bool isModify = home == null ? false : true;
//            if (!isModify) {
//                home = new RegionInfoRes ();
//            }
            System.Threading.Tasks.Task.Run (() => {
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Start ("Please wait...");
                });
//            System.Threading.Tasks.Task.Run (() => {
//                Application.RunOnMainThread (() => {
//                    MainPage.Loading.Start ("Please wait...");
//                });
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                    Dialog dialog = new Dialog ();
//                Application.RunOnMainThread (() => {
//                    MainPage.Loading.Hide ();
//                    Dialog dialog = new Dialog ();
                    FrameLayout dialogBodyView = new FrameLayout () {
                        Gravity = Gravity.Center,
                        Width = Application.GetRealWidth (500),
                        Height = Application.GetRealHeight (600),
                        Radius = 5,
                        BorderColor = SkinStyle.Current.Transparent,
                        BorderWidth = 0,
                        BackgroundColor = SkinStyle.Current.DialogColor,
                    };
                    dialog.AddChidren (dialogBodyView);
//                    FrameLayout dialogBodyView = new FrameLayout () {
//                        Gravity = Gravity.Center,
//                        Width = Application.GetRealWidth (500),
//                        Height = Application.GetRealHeight (600),
//                        Radius = 5,
//                        BorderColor = SkinStyle.Current.Transparent,
//                        BorderWidth = 0,
//                        BackgroundColor = SkinStyle.Current.DialogColor,
//                    };
//                    dialog.AddChidren (dialogBodyView);
                    Button btnTitle = new Button () {
                        Height = Application.GetRealHeight (80),
                        TextAlignment = TextAlignment.Center,
                        TextID = R.MyInternationalizationString.HonseManagement,
                        TextColor = SkinStyle.Current.DialogTextColor,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                    };
                    dialogBodyView.AddChidren (btnTitle);
//                    Button btnTitle = new Button () {
//                        Height = Application.GetRealHeight (80),
//                        TextAlignment = TextAlignment.Center,
//                        TextID = R.MyInternationalizationString.HonseManagement,
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                    };
//                    dialogBodyView.AddChidren (btnTitle);
                    Button btnZoneName = new Button () {
                        X = Application.GetRealWidth (50),
                        Y = btnTitle.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (80),
                        TextAlignment = TextAlignment.CenterLeft,
                        TextColor = SkinStyle.Current.TextColor,
                        TextID = R.MyInternationalizationString.ResidentialName
                    };
                    dialogBodyView.AddChidren (btnZoneName);
//                    Button btnZoneName = new Button () {
//                        X = Application.GetRealWidth (50),
//                        Y = btnTitle.Bottom + Application.GetRealHeight (10),
//                        Width = Application.GetRealWidth (400),
//                        Height = Application.GetRealHeight (80),
//                        TextAlignment = TextAlignment.CenterLeft,
//                        TextColor = SkinStyle.Current.TextColor,
//                        TextID = R.MyInternationalizationString.ResidentialName
//                    };
//                    dialogBodyView.AddChidren (btnZoneName);
                    EditText etZoneName = new EditText () {
                        X = Application.GetRealWidth (50),
                        Y = btnZoneName.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (80),
                        TextColor = SkinStyle.Current.TextColor,
                        TextAlignment = TextAlignment.Center,
                        Radius = 5,
                        BorderColor = SkinStyle.Current.BorderColor,
                        BorderWidth = 2,
                        Text = home.Name,
                    };
                    dialogBodyView.AddChidren (etZoneName);
                    if (MainPage.LoginUser.AccountType == 1) {
                        etZoneName.Enable = false;
                        etZoneName.BorderWidth = 0;
                    }
//                    EditText etZoneName = new EditText () {
//                        X = Application.GetRealWidth (50),
//                        Y = btnZoneName.Bottom + Application.GetRealHeight (10),
//                        Width = Application.GetRealWidth (400),
//                        Height = Application.GetRealHeight (80),
//                        TextColor = SkinStyle.Current.TextColor,
//                        TextAlignment = TextAlignment.Center,
//                        Radius = 5,
//                        BorderColor = SkinStyle.Current.BorderColor,
//                        BorderWidth = 2,
//                        Text = home.Name,
//                    };
//                    dialogBodyView.AddChidren (etZoneName);
//                    if (MainPage.LoginUser.AccountType == 1) {
//                        etZoneName.Enable = false;
//                        etZoneName.BorderWidth = 0;
//                    }
                    #region  ---绑定mac---
                    Button btnZoneMAC = new Button () {
                        X = Application.GetRealWidth (50),
                        Y = etZoneName.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (310),
                        Height = Application.GetRealHeight (80),
                        TextAlignment = TextAlignment.CenterLeft,
                        TextColor = SkinStyle.Current.TextColor,
                        TextID = R.MyInternationalizationString.GatewayMAC
                    };
                    dialogBodyView.AddChidren (btnZoneMAC);
//                    #region  ---绑定mac---
//                    Button btnZoneMAC = new Button () {
//                        X = Application.GetRealWidth (50),
//                        Y = etZoneName.Bottom + Application.GetRealHeight (10),
//                        Width = Application.GetRealWidth (310),
//                        Height = Application.GetRealHeight (80),
//                        TextAlignment = TextAlignment.CenterLeft,
//                        TextColor = SkinStyle.Current.TextColor,
//                        TextID = R.MyInternationalizationString.GatewayMAC
//                    };
//                    dialogBodyView.AddChidren (btnZoneMAC);
                    Button btnScan = new Button () {
                        X = btnZoneMAC.Right,
                        Y = etZoneName.Bottom + Application.GetRealHeight (5),
                        Width = Application.GetRealWidth (90),
                        Height = Application.GetRealHeight (90),
                        UnSelectedImagePath = "Item/Scan.png"
                    };
                    dialogBodyView.AddChidren (btnScan);
//                    Button btnScan = new Button () {
//                        X = btnZoneMAC.Right,
//                        Y = etZoneName.Bottom + Application.GetRealHeight (5),
//                        Width = Application.GetRealWidth (90),
//                        Height = Application.GetRealHeight (90),
//                        UnSelectedImagePath = "Item/Scan.png"
//                    };
//                    dialogBodyView.AddChidren (btnScan);
                    EditText etZoneMAC = new EditText () {
                        X = Application.GetRealWidth (50),
                        Y = btnZoneMAC.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (80),
                        TextAlignment = TextAlignment.Center,
                        TextColor = SkinStyle.Current.TextColor,
                        Radius = 5,
                        BorderColor = SkinStyle.Current.BorderColor,
                        BorderWidth = 2,
                        Text = gatewayMAC,
                    };
                    dialogBodyView.AddChidren (etZoneMAC);
                    btnScan.MouseUpEventHandler += (sdf, fds) => {
                        com.hdl.on.Scan.OpenScan ((scanMAC) => {
                            Application.RunOnMainThread (() => {
                                ulong scan16bit1 = 0;
                                var tryresult = ulong.TryParse (scanMAC, out scan16bit1);
                                if (tryresult) {
                                    var scan16bit = scan16bit1.ToString ("X");
                                    var count = 16 - scan16bit.Length;
                                    for (int i = 0; i < count; i++) {
                                        scan16bit = "0" + scan16bit;
                                    }
                                    etZoneMAC.Text = scan16bit;
                                } else {
                                    new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.ScanFail), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show (etZoneMAC);
                                }
                            });
                        });
                    };
                    #endregion
//                    EditText etZoneMAC = new EditText () {
//                        X = Application.GetRealWidth (50),
//                        Y = btnZoneMAC.Bottom + Application.GetRealHeight (10),
//                        Width = Application.GetRealWidth (400),
//                        Height = Application.GetRealHeight (80),
//                        TextAlignment = TextAlignment.Center,
//                        TextColor = SkinStyle.Current.TextColor,
//                        Radius = 5,
//                        BorderColor = SkinStyle.Current.BorderColor,
//                        BorderWidth = 2,
//                        Text = gatewayMAC,
//                    };
//                    dialogBodyView.AddChidren (etZoneMAC);
//                    btnScan.MouseUpEventHandler += (sdf, fds) => {
//                        com.hdl.on.Scan.OpenScan ((scanMAC) => {
//                            Application.RunOnMainThread (() => {
//                                ulong scan16bit1 = 0;
//                                var tryresult = ulong.TryParse (scanMAC, out scan16bit1);
//                                if (tryresult) {
//                                    var scan16bit = scan16bit1.ToString ("X");
//                                    var count = 16 - scan16bit.Length;
//                                    for (int i = 0; i < count; i++) {
//                                        scan16bit = "0" + scan16bit;
//                                    }
//                                    etZoneMAC.Text = scan16bit;
//                                } else {
//                                    new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.ScanFail), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show (etZoneMAC);
//                                }
//                            });
//                        });
//                    };
//                    #endregion
                    FrameLayout BottomView = new FrameLayout () {
                        Y = Application.GetRealHeight (600 - 88),
                        Height = Application.GetRealHeight (90),
                        BackgroundColor = SkinStyle.Current.Black50Transparent,
                    };
                    dialogBodyView.AddChidren (BottomView);
//                    FrameLayout BottomView = new FrameLayout () {
//                        Y = Application.GetRealHeight (600 - 88),
//                        Height = Application.GetRealHeight (90),
//                        BackgroundColor = SkinStyle.Current.Black50Transparent,
//                    };
//                    dialogBodyView.AddChidren (BottomView);
                    Button btnClose = new Button () {
                        Width = Application.GetRealWidth (250),
                        TextID = R.MyInternationalizationString.Close,
                        TextColor = SkinStyle.Current.DialogTextColor,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                    };
                    BottomView.AddChidren (btnClose);
                    btnClose.MouseUpEventHandler += (sdf, fds) => {
                        dialog.Close ();
                    };
//                    Button btnClose = new Button () {
//                        Width = Application.GetRealWidth (250),
//                        TextID = R.MyInternationalizationString.Close,
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                    };
//                    BottomView.AddChidren (btnClose);
//                    btnClose.MouseUpEventHandler += (sdf, fds) => {
//                        dialog.Close ();
//                    };
                    Button btnOption = new Button () {
                        X = btnClose.Right + Application.GetRealWidth (2),
                        Width = Application.GetRealWidth (250),
                        TextID = home.Id != UserConfig.Instance.CurrentRegion.Id ? R.MyInternationalizationString.Change : R.MyInternationalizationString.SAVE,
                        TextColor = SkinStyle.Current.DialogTextColor,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                    };
                    BottomView.AddChidren (btnOption);
                    if (!isModify) {
                        btnOption.TextID = R.MyInternationalizationString.SAVE;
                    }
                    btnOption.MouseUpEventHandler += (sdf, fds) => {
                        if (btnOption.TextID == R.MyInternationalizationString.Change) {
                            ChangeRegion (home, etZoneMAC.Text.Trim ());
                        } else {
                            var macString = etZoneMAC.Text.Trim ();
                            if (!isModify) {
                                if (etZoneName.Text.Trim () == "") {
                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.PleaseEnterZoneName), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                    return;
                                }
                                if (macString == "")//
                                {
                                    try {
                                        Convert.ToUInt64 (macString, 16);
                                    } catch {
                                        new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght),
                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                        return;
                                    }
                                    if (macString.Length != 16) {
                                        new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght),
                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                        return;
                                    }
                                }
                                home.Name = etZoneName.Text.Trim ();
                                AddHome (home.Id, macString);
                            } else {
                                //修改住宅名称
                                if (home.Name != etZoneName.Text.Trim ()) {
                                    home.Name = etZoneName.Text.Trim ();
                                    ChangeHomeName (home.Id, home.Name);
                                }
                                //修改住宅绑定的mac
                                if (macString != gatewayMAC) {
                                    ChangeHomeMACByHomeId (home.Id, macString, gatewayMAC);
                                }
                            }
                        }
                        dialog.Close ();
                    };
                    etZoneName.TextChangeEventHandler += (adsf, lkadf) => {
                        if (etZoneName.Text.Trim () != home.Name) {
                            btnOption.TextID = R.MyInternationalizationString.SAVE;
                        } else {
                            btnOption.TextID = R.MyInternationalizationString.Change;
                        }
                    };
                    dialog.Show ();
                });
            });
        }
//                    Button btnOption = new Button () {
//                        X = btnClose.Right + Application.GetRealWidth (2),
//                        Width = Application.GetRealWidth (250),
//                        TextID = home.Id != UserConfig.Instance.CurrentRegion.Id ? R.MyInternationalizationString.Change : R.MyInternationalizationString.SAVE,
//                        TextColor = SkinStyle.Current.DialogTextColor,
//                        BackgroundColor = SkinStyle.Current.DialogTitle,
//                    };
//                    BottomView.AddChidren (btnOption);
//                    if (!isModify) {
//                        btnOption.TextID = R.MyInternationalizationString.SAVE;
//                    }
//                    btnOption.MouseUpEventHandler += (sdf, fds) => {
//                        if (btnOption.TextID == R.MyInternationalizationString.Change) {
//                            ChangeRegion (home, etZoneMAC.Text.Trim ());
//                        } else {
//                            var macString = etZoneMAC.Text.Trim ();
//                            if (!isModify) {
//                                if (etZoneName.Text.Trim () == "") {
//                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.PleaseEnterZoneName), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                                    return;
//                                }
//                                if (macString == "")//
//                                {
//                                    try {
//                                        Convert.ToUInt64 (macString, 16);
//                                    } catch {
//                                        new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght),
//                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                                        return;
//                                    }
//                                    if (macString.Length != 16) {
//                                        new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght),
//                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                                        return;
//                                    }
//                                }
//                                home.Name = etZoneName.Text.Trim ();
//                                AddHome (home.Id, macString);
//                            } else {
//                                //修改住宅名称
//                                if (home.Name != etZoneName.Text.Trim ()) {
//                                    home.Name = etZoneName.Text.Trim ();
//                                    ChangeHomeName (home.Id, home.Name);
//                                }
//                                //修改住宅绑定的mac
//                                if (macString != gatewayMAC) {
//                                    ChangeHomeMACByHomeId (home.Id, macString, gatewayMAC);
//                                }
//                            }
//                        }
//                        dialog.Close ();
//                    };
//                    etZoneName.TextChangeEventHandler += (adsf, lkadf) => {
//                        if (etZoneName.Text.Trim () != home.Name) {
//                            btnOption.TextID = R.MyInternationalizationString.SAVE;
//                        } else {
//                            btnOption.TextID = R.MyInternationalizationString.Change;
//                        }
//                    };
//                    dialog.Show ();
//                });
//            });
//        }
        /// <summary>
        /// 添加住宅,需要传入住宅名称跟住宅绑定的网关mac
        /// </summary>
        /// <param name="homeName">Home name.</param>
        /// <param name="macString">Mac string.</param>
        void AddHome (string HomeId, string macString)
        {
            var mBindMacObj = new BindGatewayToHomeObj () {
                LoginAccessToken = MainPage.LoginUser.LoginTokenString,
                HomeId = HomeId,
//        /// <summary>
//        /// 添加住宅,需要传入住宅名称跟住宅绑定的网关mac
//        /// </summary>
//        /// <param name="homeName">Home name.</param>
//        /// <param name="macString">Mac string.</param>
//        void AddHome (string HomeId, string macString)
//        {
//            var mBindMacObj = new BindGatewayToHomeObj () {
//                LoginAccessToken = MainPage.LoginUser.LoginTokenString,
//                HomeId = HomeId,
            };
            mBindMacObj.BindGateways.Add (macString);
//            };
//            mBindMacObj.BindGateways.Add (macString);
//            string urlHead = MainPage.RequestHttpsHost;
//            if (mBindMacObj.IsOtherAccountCtrl) {
//                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
//                mBindMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
//            }
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
//            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
            //var requestObj21 = new AddHomeAndGatewayByONObj () { Region = homeName, MAC = macString };
            //var requestJson21 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj21);
            var revertObj21 = MainPage.RequestHttps (API.BindGatewayToHome, requestJson);
            if (revertObj21.StateCode.ToUpper () == "SUCCESS") {
                new Alert ("", Language.StringByID (R.MyInternationalizationString.addsuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                ShowHomeList ();
            } else {
                ShowBindGatewayToHomeErrorInfo (revertObj21.StateCode);
            }
        }
//            //var requestObj21 = new AddHomeAndGatewayByONObj () { Region = homeName, MAC = macString };
//            //var requestJson21 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj21);
//            var revertObj21 = MainPage.RequestHttps (API.BindGatewayToHome, requestJson, urlHead);
//            if (revertObj21.StateCode.ToUpper () == "SUCCESS") {
//                new Alert ("", Language.StringByID (R.MyInternationalizationString.addsuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                ShowHomeList ();
//            } else {
//                ShowBindGatewayToHomeErrorInfo (revertObj21.StateCode);
//            }
//        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowBindGatewayToHomeErrorInfo (string stateCodeStr)
        {
            //string mes = "";
            ////2020-01-06 待补充
            //if (stateCodeStr == ErrorCode.NetworkError) {
            //    mes = ErrorCode.NetworkError;
            //} else {
            //    mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
//        /// <summary>
//        ///
//        /// </summary>
//        /// <param name="stateCodeStr"></param>
//        void ShowBindGatewayToHomeErrorInfo (string stateCodeStr)
//        {
//            //string mes = "";
//            ////2020-01-06 待补充
//            //if (stateCodeStr == ErrorCode.NetworkError) {
//            //    mes = ErrorCode.NetworkError;
//            //} else {
//            //    mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
            //}
            //if (!string.IsNullOrEmpty (mes)) {
            //    Application.RunOnMainThread (() => {
            //        new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
            //    });
            //}
//            //}
//            //if (!string.IsNullOrEmpty (mes)) {
//            //    Application.RunOnMainThread (() => {
//            //        new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//            //    });
//            //}
            Application.RunOnMainThread (() => {
                new Alert ("", "Bind failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
            });
        }
//            Application.RunOnMainThread (() => {
//                new Alert ("", "Bind failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//            });
//        }
        void loadHomeLists ()
        {
            var requestObj2 = new GetHomePagerObj ();
            requestObj2.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
            var revertObj2 = MainPage.RequestHttps (API.GetHomePager, requestJson2);
            if (revertObj2.StateCode.ToUpper() == "SUCCESS") {
                var mResidenceRes = Newtonsoft.Json.JsonConvert.DeserializeObject<ResidenceRes> (revertObj2.ResponseData.ToString ());
//        void loadHomeLists ()
//        {
//            var requestObj2 = new GetHomePagerObj ();
//            requestObj2.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
//            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
//            var revertObj2 = MainPage.RequestHttps (API.GetHomePager, requestJson2);
//            if (revertObj2.StateCode.ToUpper() == "SUCCESS") {
//                var mResidenceRes = Newtonsoft.Json.JsonConvert.DeserializeObject<ResidenceRes> (revertObj2.ResponseData.ToString ());
                if (mResidenceRes.PageData != null && mResidenceRes.PageData.Count > 0) {
                    UserConfig.Instance.HomeLists = mResidenceRes.PageData;
                    UserConfig.Instance.SaveUserConfig ();
                }
            } else {
                ShowGetHomePagerErrorInfo (revertObj2.StateCode);
            }
        }
//                if (mResidenceRes.PageData != null && mResidenceRes.PageData.Count > 0) {
//                    UserConfig.Instance.HomeLists = mResidenceRes.PageData;
//                    UserConfig.Instance.SaveUserConfig ();
//                }
//            } else {
//                ShowGetHomePagerErrorInfo (revertObj2.StateCode);
//            }
//        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowGetHomePagerErrorInfo (string stateCodeStr)
        {
            string mes = "";
            //2020-01-06 待补充
            if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
//        /// <summary>
//        ///
//        /// </summary>
//        /// <param name="stateCodeStr"></param>
//        void ShowGetHomePagerErrorInfo (string stateCodeStr)
//        {
//            string mes = "";
//            //2020-01-06 待补充
//            if (stateCodeStr == ErrorCode.NetworkError) {
//                mes = ErrorCode.NetworkError;
//            } else {
//                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
//            }
//            if (!string.IsNullOrEmpty (mes)) {
//                Application.RunOnMainThread (() => {
//                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                });
//            }
        }
//        }
        /// <summary>
        /// 修改住宅名称
        /// </summary>
        void ChangeHomeName (string homeID, string homeName)
        {
            if (UserConfig.Instance.CurrentRegion.Name == homeName)
                return;
            var requestObj2 = new EditHomeObj () { HomeId = homeID, Name = homeName };
            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
            var revertObj2 = MainPage.RequestHttps (API.EditHome, requestJson2);
            if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
                new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                UserConfig.Instance.SaveUserConfig ();
                this.ShowHomeList ();
                if (homeID == UserConfig.Instance.CurrentRegion.Id) {
                    IO.FileUtils.ReNameFoler (UserConfig.Instance.CurrentRegion.Name, homeName);
                    UserConfig.Instance.CurrentRegion.Name = homeName;
                }
            } else {
                ShowEditHomeErrorInfo (revertObj2.StateCode);
            }
        }
//        /// <summary>
//        /// 修改住宅名称
//        /// </summary>
//        void ChangeHomeName (string homeID, string homeName)
//        {
//            if (UserConfig.Instance.CurrentRegion.Name == homeName)
//                return;
//            var requestObj2 = new EditHomeObj () {
//                HomeId = homeID,
//                Name = homeName,
//                LoginAccessToken = MainPage.LoginUser.LoginTokenString
//            };
//            string urlHead = MainPage.RequestHttpsHost;
//            if (requestObj2.IsOtherAccountCtrl) {
//                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
//                requestObj2.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
//            }
//            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
//            var revertObj2 = MainPage.RequestHttps (API.EditHome, requestJson2, urlHead);
//            if (revertObj2.StateCode.ToUpper () == "SUCCESS") {
//                new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                UserConfig.Instance.SaveUserConfig ();
//                this.ShowHomeList ();
//                if (homeID == UserConfig.Instance.CurrentRegion.Id) {
//                    IO.FileUtils.ReNameFoler (UserConfig.Instance.CurrentRegion.Name, homeName);
//                    UserConfig.Instance.CurrentRegion.Name = homeName;
//                }
//            } else {
//                ShowEditHomeErrorInfo (revertObj2.StateCode);
//            }
//        }
        void ShowEditHomeErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == "NoLogin") {
                mes = ErrorCode.NoLogin;
            } else if (stateCodeStr == "Exist") {
                mes = ErrorCode.NameExist;
            } else if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
                mes = ErrorCode.HomeIdAndTokenNoConsistent;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                //RequestServerFailed
                //mes = ErrorCode.AddFailed;
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
//        void ShowEditHomeErrorInfo (string stateCodeStr)
//        {
//            string mes = "";
//            if (stateCodeStr == "NoLogin") {
//                mes = ErrorCode.NoLogin;
//            } else if (stateCodeStr == "Exist") {
//                mes = ErrorCode.NameExist;
//            } else if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
//                mes = ErrorCode.HomeIdAndTokenNoConsistent;
//            } else if (stateCodeStr == ErrorCode.NetworkError) {
//                mes = ErrorCode.NetworkError;
//            } else {
//                //RequestServerFailed
//                //mes = ErrorCode.AddFailed;
//                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
        }
//            }
//            if (!string.IsNullOrEmpty (mes)) {
//                Application.RunOnMainThread (() => {
//                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                });
//            }
//        }
        void ChangeHomeMACByHomeId (string homeId, string macString, string oldMacString)
        {
            var mUpdateMacObj = new UpdateMacObj () {
                LoginAccessToken = MainPage.LoginUser.LoginTokenString,
                HomeId = homeId,
                NewMac = macString,
                OldMac = oldMacString
            };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mUpdateMacObj);
            //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = homeId, MAC = macString, IsReBind = false });
            var respone = MainPage.RequestHttps (API.UpdateMac, requestJson);
            if (respone.StateCode.ToUpper() == "SUCCESS") {
                new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                UserConfig.Instance.SaveUserConfig ();
                this.ShowHomeList ();
            } else {
                //2020-01-06 错误提示
                ShowUpdateMacErrorInfo (respone.StateCode);
//        void ChangeHomeMACByHomeId (string homeId, string macString, string oldMacString)
//        {
//            var mUpdateMacObj = new UpdateMacObj () {
//                LoginAccessToken = MainPage.LoginUser.LoginTokenString,
//                HomeId = homeId,
//                NewMac = macString,
//                OldMac = oldMacString
//            };
//            string urlHead = MainPage.RequestHttpsHost;
//            if (mUpdateMacObj.IsOtherAccountCtrl) {
//                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
//                mUpdateMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
//            }
//            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mUpdateMacObj);
//            //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = homeId, MAC = macString, IsReBind = false });
//            var respone = MainPage.RequestHttps (API.UpdateMac, requestJson, urlHead);
//            if (respone.StateCode.ToUpper() == "SUCCESS") {
//                new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                UserConfig.Instance.SaveUserConfig ();
//                this.ShowHomeList ();
//            } else {
//                //2020-01-06 错误提示
//                ShowUpdateMacErrorInfo (respone.StateCode);
            }
        }
//            }
//        }
        /// <summary>
        //*    (1)Success 则 [调用此接口操作成功], ResponseData则为null<br/>
        //*    (2)ParameterOrEmpty,则响应字段中 [ErrorInfo] 为错误信息, ResponseData则为null<br/>
        //*    (3)NoLogin,则响应字段中 [ErrorInfo] 为错误信息为 [无效登录Token!]<br/>
        //*    (4)NoRecord,则响应字段中 [ErrorInfo] 为错误信息为 [未到相关记录, 请确认您提交参数是否正确!]<br/>
        //*    (5)NoBind,则响应字段中 [ErrorInfo] 为错误信息为 [当前提交HomeId与当前OldMac尚不存在绑定关系!]<br/>
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowUpdateMacErrorInfo (string stateCodeStr)
        {
            string mes = "";
            //2020-01-06 代补充
            if (stateCodeStr == "NoBind") {
                mes = ErrorCode.NoBind;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
//        /// <summary>
//        //*    (1)Success 则 [调用此接口操作成功], ResponseData则为null<br/>
//        //*    (2)ParameterOrEmpty,则响应字段中 [ErrorInfo] 为错误信息, ResponseData则为null<br/>
//        //*    (3)NoLogin,则响应字段中 [ErrorInfo] 为错误信息为 [无效登录Token!]<br/>
//        //*    (4)NoRecord,则响应字段中 [ErrorInfo] 为错误信息为 [未到相关记录, 请确认您提交参数是否正确!]<br/>
//        //*    (5)NoBind,则响应字段中 [ErrorInfo] 为错误信息为 [当前提交HomeId与当前OldMac尚不存在绑定关系!]<br/>
//        /// </summary>
//        /// <param name="stateCodeStr"></param>
//        void ShowUpdateMacErrorInfo (string stateCodeStr)
//        {
//            string mes = "";
//            //2020-01-06 代补充
//            if (stateCodeStr == "NoBind") {
//                mes = ErrorCode.NoBind;
//            } else if (stateCodeStr == ErrorCode.NetworkError) {
//                mes = ErrorCode.NetworkError;
//            } else {
//                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
//            }
//            if (!string.IsNullOrEmpty (mes)) {
//                Application.RunOnMainThread (() => {
//                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//                });
//            }
        }
//        }
        ///// <summary>
        ///// 修改住宅mac
        ///// </summary>
        //void ChangeHomeMAC (int macID, string macString)
        //{
        //    var requestObj2 = new EditMACByONObj { Id = macID, MAC = macString };
        //    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
        //    var revertObj2 = MainPage.RequestHttps ("EditMACByON", requestJson2);
        //    if (revertObj2.StateCode.ToUpper() == "SUCCESS") {
        //        new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //        UserConfig.Instance.SaveUserConfig ();
        //        this.ShowHomeList ();
        //    }
        //}
    }
}
//        ///// <summary>
//        ///// 修改住宅mac
//        ///// </summary>
//        //void ChangeHomeMAC (int macID, string macString)
//        //{
//        //    var requestObj2 = new EditMACByONObj { Id = macID, MAC = macString };
//        //    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
//        //    var revertObj2 = MainPage.RequestHttps ("EditMACByON", requestJson2);
//        //    if (revertObj2.StateCode.ToUpper() == "SUCCESS") {
//        //        new Alert ("", Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
//        //        UserConfig.Instance.SaveUserConfig ();
//        //        this.ShowHomeList ();
//        //    }
//        //}
//    }
//}