using System;
using System.Collections.Generic;
using AVFoundation;
using CoreGraphics;
using UIKit;

namespace HDL_ON
{
    public class ZXingOverlayView : UIView
    {
        public UILabel textBottom;
        UIView redLine;
        public Action OnCancel;
        public Action OnTorch;
        public UIBarButtonItem cancelButton;
        public UIBarButtonItem torchButton;
        public string cancelText = "取消";
        public string flashText = "闪光灯";
        public string bottomText = "扫一扫";

        CGSize screenSize = UIScreen.MainScreen.Bounds.Size;

        public ZXingOverlayView(string cancelText = "取消", string flashText = "闪光灯", string titleText = "扫一扫") : base()
        {
            this.cancelText = cancelText;
            this.flashText = flashText;
            this.bottomText = titleText;

            Opaque = false;
            BackgroundColor = UIColor.Clear;

            textBottom = new UILabel()
            {
                Frame = new CGRect(0, (screenSize.Height - 44 + screenSize.Width * 0.75f) / 2, screenSize.Width, 40f),
                Text = bottomText,
                Font = UIFont.SystemFontOfSize(13),
                TextAlignment = UITextAlignment.Center,
                TextColor = UIColor.White,
                Lines = 0,
                BackgroundColor = UIColor.Clear
            };
            this.AddSubview(textBottom);

            var captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);

            bool hasTorch = false;

            if (captureDevice != null)
                hasTorch = captureDevice.TorchAvailable;

            InvokeOnMainThread(delegate {
                // Setting tool bar
                var toolBar = new UIToolbar(new CGRect(0, Frame.Height - 44, Frame.Width, 44));

                var buttons = new List<UIBarButtonItem>();
                cancelButton = new UIBarButtonItem(cancelText, UIBarButtonItemStyle.Done, delegate { OnCancel?.Invoke(); });
                cancelButton.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
                buttons.Add(cancelButton);
                if (hasTorch)
                {
                    buttons.Add(new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace));

                    torchButton = new UIBarButtonItem(flashText, UIBarButtonItemStyle.Done, delegate { OnTorch?.Invoke(); });
                    torchButton.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
                    buttons.Add(torchButton);
                }

                toolBar.Items = buttons.ToArray();
                toolBar.BarTintColor = UIColor.Clear;
                toolBar.Translucent = true;
                toolBar.TintColor = UIColor.Black;
                toolBar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin;
                Add(toolBar);
            });

        }

        public override void Draw(CGRect rect)
        {
            CGRect screenDrawRect = new CGRect(0, 0, screenSize.Width, screenSize.Height - 44);

            //中间清空的矩形框
            CGRect clearDrawRect = new CGRect(screenDrawRect.Size.Width * 0.125f, (screenDrawRect.Size.Height - screenDrawRect.Size.Width * 0.75f) / 2,
                                              screenDrawRect.Size.Width * 0.75f, screenDrawRect.Size.Width * 0.75f);

            CGContext ctx = UIGraphics.GetCurrentContext();
            AddScreenFillRect(ctx, screenDrawRect);
            AddCenterClearRect(ctx, clearDrawRect);
            AddWhiteRect(ctx, clearDrawRect);
            AddCornerLineWithContext(ctx, clearDrawRect);
        }
        //添加屏幕半透明填充色
        private void AddScreenFillRect(CGContext ctx, CGRect rect)
        {
            ctx.SetFillColor(new CGColor(0, 0, 0, 0.5f));
            ctx.FillRect(rect);
        }
        //添加中心全透明填充色
        private void AddCenterClearRect(CGContext ctx, CGRect rect)
        {
            ctx.ClearRect(rect);
        }
        //根据点画线
        private void AddLine(CGPoint[] pointA, CGPoint[] pointB, CGContext ctx)
        {
            ctx.SetLineWidth(2f);
            ctx.AddLines(pointA);
            ctx.AddLines(pointB);
        }
        //添加白色方框
        private void AddWhiteRect(CGContext ctx, CGRect rect)
        {
            ctx.StrokeRect(rect);
            ctx.SetStrokeColor(new CGColor(1, 1, 1, 1));
            ctx.SetLineWidth(0.8f);
            ctx.AddRect(rect);
            ctx.StrokePath();
        }
        //添加四个角的蓝色
        private void AddCornerLineWithContext(CGContext ctx, CGRect rect)
        {
            //画四个边角
            ctx.SetLineWidth(2f);
            ctx.SetStrokeColor(UIColor.FromRGB(22, 118, 188).CGColor);

            //左上角
            CGPoint[] poinsTopLeftA = new CGPoint[] { new CGPoint(rect.Location.X + 0.7f, rect.Location.Y), new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + 15f) };

            CGPoint[] poinsTopLeftB = new CGPoint[] { new CGPoint(rect.Location.X, rect.Location.Y + 0.7f), new CGPoint(rect.Location.X + 15f, rect.Location.Y + 0.7f) };

            AddLine(poinsTopLeftA, poinsTopLeftB, ctx);

            //左下角
            CGPoint[] poinsBottomLeftA = new CGPoint[] { new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + rect.Size.Height - 15f), new CGPoint(rect.Location.X + 0.7f, rect.Location.Y + rect.Size.Height) };

            CGPoint[] poinsBottomLeftB = new CGPoint[] { new CGPoint(rect.Location.X, rect.Location.Y + rect.Size.Height - 0.7f), new CGPoint(rect.Location.X + 0.7f + 15f, rect.Location.Y + rect.Size.Height - 0.7f) };

            AddLine(poinsBottomLeftA, poinsBottomLeftB, ctx);

            //右上角
            CGPoint[] poinsTopRightA = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 15f, rect.Location.Y + 0.7f), new CGPoint(rect.Location.X + rect.Size.Width, rect.Location.Y + 0.7) };

            CGPoint[] poinsTopRightB = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y), new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y + 15 + 0.7) };

            AddLine(poinsTopRightA, poinsTopRightB, ctx);

            CGPoint[] poinsBottomRightA = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 0.7f, rect.Location.Y + rect.Size.Height - 15f), new CGPoint(rect.Location.X - 0.7f + rect.Size.Width, rect.Location.Y + rect.Size.Height) };

            CGPoint[] poinsBottomRightB = new CGPoint[] { new CGPoint(rect.Location.X + rect.Size.Width - 15f, rect.Location.Y + rect.Size.Height - 0.7f), new CGPoint(rect.Location.X + rect.Size.Width, rect.Location.Y + rect.Size.Height - 0.7f) };

            AddLine(poinsBottomRightA, poinsBottomRightB, ctx);

            ctx.StrokePath();
        }

        public void Destroy()
        {
            InvokeOnMainThread(() => {
                textBottom.RemoveFromSuperview();
                redLine.RemoveFromSuperview();

                textBottom = null;
                redLine = null;
            });
        }

    }
}