public InvoiceView(UIViewController parent)
            {
                BackgroundColor = Theme.Current.BackgroundGray;
                //Add(backgroundView = new UIImageView(UIImage.FromBundle("PaymentBG").Blur(50)));
                Add(InvoiceTable = new InvoiceTableView());
                Add(SideBar      = new InvoiceSideBar {
                    Checkout = () => {
                        Parent.Checkout();
                    }
                });
                Add(BottomView = new InvoiceBottomView(parent)
                {
                    AddItem = async(i) => {
                        var c = i as Coupon;
                        if (c != null && c.ManualDiscount)
                        {
                            var amount = await AskForValue("Coupon Amount", "");
                            if (amount == 0)
                            {
                                return;
                            }
                            i.Price = amount;
                        }
                        Invoice.AddItem(i);
                    }
                });

                this.ConstrainLayout(() =>
//					backgroundView.Frame.X == Frame.X &&
//					backgroundView.Frame.Width == Frame.Width &&
//					backgroundView.Frame.Height == Frame.Height &&
//					backgroundView.Frame.Top == Frame.Top &&

                                     BottomView.Frame.Right == Frame.Right &&
                                     BottomView.Frame.Width == Frame.Width &&
                                     BottomView.Frame.Bottom == Frame.Bottom &&
                                     BottomView.Frame.Height == bottomHeight

                                     &&

                                     SideBar.Frame.Right == Frame.Right &&
                                     SideBar.Frame.Width == sideBarWidth &&
                                     SideBar.Frame.Bottom == BottomView.Frame.Top &&
                                     SideBar.Frame.Top == Frame.Top

                                     &&

                                     InvoiceTable.Frame.Left == Frame.Left &&
                                     InvoiceTable.Frame.Right == SideBar.Frame.Left &&
                                     InvoiceTable.Frame.Bottom == BottomView.Frame.Top &&
                                     InvoiceTable.Frame.Top == Frame.Top
                                     );
            }
        public InvoiceViewController()
        {
            SetTitle();
            Settings.Shared.SubscribeToProperty("TestMode", SetTitle);
            var searchBar = new ItemSearchView {
                Frame = new System.Drawing.RectangleF(0, 0, 200, 30)
            };

            searchBar.ItemSelected += (Item obj) => {
                Invoice.AddItem(obj);
            };
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(searchBar);
            NavigationItem.LeftBarButtonItem  = new UIBarButtonItem(UIImage.FromBundle("menu").ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIBarButtonItemStyle.Plain, (s, e) => {
                //Show simple actionsheet
                if (sheet != null)
                {
                    sheet.DismissWithClickedButtonIndex(-1, true);
                    return;
                }
                sheet = new SimpleActionSheet {
                    { "New Invoice", async() => {
                          if (await AskSave())
                          {
                              NewInvoice();
                          }
                      } },
                    { "Load Invoice", () => {
                          if (popover != null)
                          {
                              popover.Dismiss(true);
                          }
                          popover = new UIPopoverController(new UINavigationController(new LoadInvoiceViewController()
                            {
                                InvoiceSelected = async(i) => {
                                    popover.Dismiss(true);
                                    try{
                                        BigTed.BTProgressHUD.ShowContinuousProgress();
                                        if (Invoice != null && Invoice.Id != i.Id)
                                        {
                                            if (!await AskSave())
                                            {
                                                return;
                                            }
                                        }
                                        Invoice.DeleteLocal();
                                        Invoice = await WebService.Main.GetInvoice(i.Id);
                                        Invoice.Save(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                    finally{
                                        BigTed.BTProgressHUD.Dismiss();
                                    }
                                },
                            }));
                          popover.DidDismiss += (sender, evt) => {
                              popover.Dispose();
                          };
                          popover.PresentFromBarButtonItem(NavigationItem.LeftBarButtonItem, UIPopoverArrowDirection.Any, true);
                      } },
                    { "Payout Buy", () => {
                          if (popover != null)
                          {
                              popover.Dismiss(true);
                          }
                          popover = new UIPopoverController(new UINavigationController(new LoadBuyPayoutViewController()
                            {
                                InvoiceSelected = async(i) => {
                                    popover.Dismiss(true);
                                    try{
                                        BigTed.BTProgressHUD.ShowContinuousProgress();
                                        if (Invoice != null && Invoice.Id != i.Id)
                                        {
                                            if (!await AskSave())
                                            {
                                                return;
                                            }
                                        }
                                        Invoice.DeleteLocal();
                                        Invoice = await WebService.Main.GetInvoice(i.Id);
                                        //Setup payments
                                        Database.Main.Table <PaymentType> ().Where(x => x.IsActive)
                                        .OrderBy(X => X.SortOrder).ToList().ForEach(x => Invoice.Payments.Add(new Payment {
                                            PaymentType = x
                                        }));
                                        Invoice.Save(true);
                                        if ((i as BuyInvoice).IsOnAccount)
                                        {
                                            Invoice.OnAccountPayment.Amount = Invoice.Total;
                                        }
                                        else
                                        {
                                            Invoice.CashPayment.Amount = Invoice.Total;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                    finally{
                                        BigTed.BTProgressHUD.Dismiss();
                                    }
                                },
                            }));
                          popover.DidDismiss += (sender, evt) => {
                              popover.Dispose();
                          };
                          popover.PresentFromBarButtonItem(NavigationItem.LeftBarButtonItem, UIPopoverArrowDirection.Any, true);
                      } },
                    { "Settings", () => this.PresentViewControllerAsync(new UINavigationController(new SettingsViewController()), true) },
                };
                sheet.Dismissed += (object sender, UIButtonEventArgs e2) => {
                    sheet.Dispose();
                    sheet = null;
                };
                sheet.ShowFrom(s as UIBarButtonItem, true);
            });

            //this.AutomaticallyAdjustsScrollViewInsets = false;
        }