using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using NZSDK_CSharpSample.Properties;
using System.Runtime.InteropServices;

namespace NZSDK_CSharpSample
{
    public partial class NZLiveDisplayForm : Form
    {
        #region Fields

        NZLiveDisplay sdk = new NZLiveDisplay();
        NZLiveDisplay.DrawVideoDelegate drawCallback;

        #endregion

        #region Constructors

        public NZLiveDisplayForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        #endregion

        #region Private / Protected Methods

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            sdk.SetDisplayWindow(pictureBox1.Handle);
            sdk.EnableDisplayWindow(false);

            string directoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string logoFile = Path.Combine(directoryPath, "logo.jpg");
            sdk.SetLogoImage(logoFile);
            pictureBox1.Image = Image.FromFile(logoFile);
            sdk.SetOSDFont("Arial", 15, Color.White, Color.Black);
            drawCallback = new NZLiveDisplay.DrawVideoDelegate(OnDrawVideo);
            sdk.SetCustomDrawHandler(drawCallback, IntPtr.Zero);

            if (String.IsNullOrEmpty(Settings.Default.NZLD_Address))
                LoadDefaultConnection();
            else
            {
                txtAddress.Text = Settings.Default.NZLD_Address;
                txtPort.Text = Settings.Default.NZLD_Port;
                txtUrl.Text = Settings.Default.NZLD_Url;
                txtLoginId.Text = Settings.Default.NZLD_LoginId;
                txtPassword.Text = Settings.Default.NZLD_Password;
            }
        }

        protected bool OnDrawVideo(int channel, IntPtr pBitmap, IntPtr pSrcRect, IntPtr pUserParam)
        {
            NZLiveDisplay.GdiBitmap gdiBitmap = (NZLiveDisplay.GdiBitmap)Marshal.PtrToStructure(pBitmap, typeof(NZLiveDisplay.GdiBitmap));
            Bitmap bitmap = new Bitmap(gdiBitmap.bmWidth,
                gdiBitmap.bmHeight,
                gdiBitmap.bmWidthBytes,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb,
                gdiBitmap.bmBits);

            Graphics g = Graphics.FromImage(bitmap);
            SolidBrush brush = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
            g.FillRectangle(brush, 0, 0, 80, 80);
            return true;
        }

        private void SaveConnection()
        {
            Settings.Default.NZLD_Address = txtAddress.Text;
            Settings.Default.NZLD_Port = txtPort.Text;
            Settings.Default.NZLD_Url = txtUrl.Text;
            Settings.Default.NZLD_LoginId = txtLoginId.Text;
            Settings.Default.NZLD_Password = txtPassword.Text;

            Settings.Default.Save();
        }

        private void LoadDefaultConnection()
        {
            txtAddress.Text = Settings.Default.NZLD_DefaultAddress;
            txtPort.Text = Settings.Default.NZLD_DefaultPort;
            txtUrl.Text = Settings.Default.NZLD_DefaultUrl;
            txtLoginId.Text = Settings.Default.NZLD_DefaultLoginId;
            txtPassword.Text = Settings.Default.NZLD_DefaultPassword;
        }

        #endregion

        #region Public Methods

        #endregion

        #region Event Handlers

        private void butConnect_Click(object sender, EventArgs e)
        {
            sdk.SetOSDText("카메라 이름", NZLiveDisplay.OSDPosition.LeftTop);
            sdk.EnableScreenPtz(true);

            sdk.EnableDisplayWindow(true);
            sdk.DrawConnectingOSD();
            sdk.OpenVideo(txtAddress.Text, Int32.Parse(txtPort.Text), txtUrl.Text, txtLoginId.Text, txtPassword.Text);

            SaveConnection();
        }

        private void butDisconnect_Click(object sender, EventArgs e)
        {
            sdk.CloseVideo();
            sdk.EnableScreenPtz(false);
            sdk.EnableDisplayWindow(false);
        }

        private void pictureBox1_SizeChanged(object sender, EventArgs e)
        {
            sdk.UpdateDisplayWindowPosition();
        }

        private void butDefault_Click(object sender, EventArgs e)
        {
            LoadDefaultConnection();
        }

        #endregion
    }
}
