using System.Drawing.Imaging;
using DummyCursor.Spy;
using Timer = System.Threading.Timer;

namespace DummyCursor
{
    public partial class MainForm : Form
    {
        private const int DummySize = 128;

        private nint _hWndWatch;
        private nint _hWndShowDummy;



        private Form _dummy = new Form()
        {
            FormBorderStyle = FormBorderStyle.None,
            ControlBox = false,
            Width = DummySize,
            Height = DummySize,
            BackColor = Color.White,
            TransparencyKey = Color.White,
            MinimumSize = Size.Empty,
            TopMost = true,
            BackgroundImageLayout = ImageLayout.Center
        };

        public MainForm()
        {
            InitializeComponent();
        }


        private void ChooseWatchingWindowButton_Click(object sender, EventArgs e)
        {
            SpyAgent sa = null;
            sa = new SpyAgent(w =>
            {
                _hWndWatch = w.Handle;
                ChooseWatchingWindowButton.Text = w.Caption;
                sa!.EndSpying();
            });
            sa.BeginSpying();

        }

        private void ChooseShowDummyOnWindowButton_Click(object sender, EventArgs e)
        {
            SpyAgent sa = null;
            sa = new SpyAgent(w =>
            {
                _hWndShowDummy = w.Handle;
                ChooseShowDummyOnWindowButton.Text = w.Caption;
                sa!.EndSpying();
            });
            sa.BeginSpying();
        }

        private void ActiveCheckButton_CheckedChanged(object sender, EventArgs e)
        {
            _dummy.Visible = ActiveCheckButton.Checked;
            if (_dummy.Visible)
            {
                var oldBmp = _dummy.BackgroundImage;

                using var cur = Icon.FromHandle(Cursor.Current.Handle).ToBitmap();
                var newBmp = new Bitmap(DummySize, DummySize, PixelFormat.Format32bppArgb);
                using var grp = Graphics.FromImage(newBmp);

                grp.DrawImage(cur, DummySize / 2f - 0, DummySize / 2f - 1, new RectangleF(0, 0, cur.Width, cur.Height), GraphicsUnit.Pixel);

                grp.DrawLine(Pens.White, 0, DummySize / 2, DummySize, DummySize / 2);
                grp.DrawLine(Pens.Black, 0, DummySize / 2 - 1, DummySize, DummySize / 2 - 1);
                grp.DrawLine(Pens.Black, 0, DummySize / 2 + 1, DummySize, DummySize / 2 + 1);

                grp.DrawLine(Pens.White, DummySize / 2, 0, DummySize / 2, DummySize);
                grp.DrawLine(Pens.Black, DummySize / 2 - 1, 0, DummySize / 2 - 1, DummySize);
                grp.DrawLine(Pens.Black, DummySize / 2 + 1, 0, DummySize / 2 + 1, DummySize);


                _dummy.BackgroundImage = newBmp;

                oldBmp?.Dispose();

                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
            }
        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_hWndWatch == 0) return;
            if (_hWndShowDummy == 0) return;

            //check mouse in watching window
            var wndWatch = new SpiedWindow(_hWndWatch);
            var rcWatch = wndWatch.ClientArea;

            var mPos = MousePosition;
            if (rcWatch.Contains(mPos) == false)
            {
                return;
            }

            int bp = 0;

            //calc % of w and h
            var wcx = mPos.X - rcWatch.X;
            var wcy = mPos.Y - rcWatch.Y;

            var percentX = (float)wcx / rcWatch.Width;
            var percentY = (float)wcy / rcWatch.Height;


            //calc show target x and y
            var wndShow = new SpiedWindow(_hWndShowDummy);
            var rcShow = wndShow.Area;

            var showX = rcShow.X + rcShow.Width * percentX - DummySize / 2f;
            var showY = rcShow.Y + rcShow.Height * percentY - DummySize / 2f;

            //move dummy to show
            _dummy.Location = new Point((int)showX, (int)showY);
        }
    }
}