﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Azure.NotificationHubs;

namespace CloudClippy.WinForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Properties.Settings.Default.SharingKey))
            {
                ResetCode_Click(null, null);
            }
            else
            {
                SetupSharingCode(Properties.Settings.Default.SharingKey);
            }
        }

        private void SetupSharingCode(string sharingCode)
        {
            var qrWriter = new ZXing.QrCode.QRCodeWriter();
            var qrCode = qrWriter.encode(sharingCode, ZXing.BarcodeFormat.QR_CODE, 400, 400);
            var barCodeWriter = new ZXing.BarcodeWriter();
            var barCode = barCodeWriter.Write(qrCode);
            pictureBox1.Image = barCode;
        }

        private void ResetCode_Click(object sender, EventArgs e)
        {
            var builder = new UriBuilder
            {
                Scheme = "clippy",
                Host = Guid.NewGuid().ToString("D")
            };
            Properties.Settings.Default.SharingKey = builder.Uri.ToString();
            Properties.Settings.Default.Save();
            SetupSharingCode(Properties.Settings.Default.SharingKey);
        }
    }
}
