using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MicroPdf417Net;

namespace CSharpDemo
{
   public partial class Form1 : Form
   {
      private Image SourcePicture = null;
      private EncodingInfo[] Info;
      private int CodePage;
      private bool decodeOK = false;
      private bool optionsChanged = false;
      private Bitmap Picture = null;

      MicroPdf417Decoder decoder = new MicroPdf417Decoder(3000, 4000);
      public Form1()
      {
         InitializeComponent();

         SetDefaultOptions();
         InitializeCodePages();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         ClearForm();
         DialogResult R = openFileDialog1.ShowDialog();
         if (R == DialogResult.OK)
         {
            SourcePicture = Image.FromFile(openFileDialog1.FileName);
            Img.Image = SourcePicture;
            LabelFileName.Text = openFileDialog1.FileName;
            button2.Enabled = true;

            button2_Click(null, null);
         }
      }

      private void ClearForm()
      {
         TimeEdit.Text = "";
         rrEdit.Text = "";
         textBox2.Text = ""; 
      }

      private void SetDefaultOptions()
      {
      }

      private void InitializeCodePages()
      {
         Info = Encoding.GetEncodings();

         for (int i = 0; i < Info.Length; i++)
         {
            String S;
            Encoding E = Console.Out.Encoding;
            int CP = Info[i].CodePage;
            S = CP.ToString() + " - " + Info[i].DisplayName;
            comboBox1.Items.Add(S);
            if (E.CodePage == CP)
            {
               comboBox1.SelectedIndex = i;
               CodePage = CP;
            }
         }
      }

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         int Index = comboBox1.SelectedIndex;
         CodePage = Info[Index].CodePage;
         String msg;
         if ((decoder != null) && (decoder.SymbolCount() > 0))
         {
            msg = decoder.GetSymbolData(CodePage);
            textBox1.Text = msg;
         }
      }

      private void button2_Click(object sender, EventArgs e)
      {
         ClearForm();
         OptionsToDecoder();
         if (Picture != null)
         {
            Picture.Dispose();
            Picture = null;
         }
         Picture = new Bitmap(SourcePicture);

         DateTime d1 = DateTime.Now;
         decodeOK = decoder.DecodeImage(Picture);
         DateTime d2 = DateTime.Now;
         TimeSpan diff = d2 - d1;
         int timeMlsc = (int)diff.TotalMilliseconds;

         TimeEdit.Text = timeMlsc.ToString();

         if(decodeOK)
         {
            rrEdit.Text = "OK";
            textBox2.Text = decoder.GetSymbolData(CodePage);

            DrawCorners();
         }
         else
         {
            if (decoder.RejectionReasons() == TMPDF_Rejection_Reasons.PDF417_DISCONNECTED)
               rrEdit.Text = "Decoder Disconnected";
            else if (decoder.RejectionReasons() == TMPDF_Rejection_Reasons.PDF417_RS_Error)
               rrEdit.Text = "Reed Solomon Error";
            else if (decoder.RejectionReasons() == TMPDF_Rejection_Reasons.PDF417_RowCount_Error)
               rrEdit.Text = "RowCount Error";
            else
               rrEdit.Text = "MPDF not Found";
         }
      }

      private void OptionsToDecoder()
      {
         //RESERVED FOR FUTURE!
         if (!optionsChanged) return;
         MicroPdf417Options opt = new MicroPdf417Options();
         
         decoder.SetOptions(opt);
         optionsChanged = false;
      }

      private void DrawCorners()
      {
         float X1, X2, Y1, Y2;
         int I1, I2, I;
         Pen P;
         float wid = Picture.Width / 600 + 3;
         P = new Pen(Color.Lime, wid);
         Graphics G = Graphics.FromImage(Picture);
         int[] RowCols = decoder.GetSymbolRect();
         for (I = 0; I < 4; I++)
         {
            I1 = (I * 2 + 0);
            I2 = (I * 2 + 2);
            if (I2 > 7) I2 = 0;
            X1 = (float)RowCols[I1 + 1];
            Y1 = (float)RowCols[I1 + 0];
            X2 = (float)RowCols[I2 + 1];
            Y2 = (float)RowCols[I2 + 0];
            G.DrawLine(P, X1, Y1, X2, Y2);
         }

         Img.Image = Picture;
      }

      private void SpeedComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
         optionsChanged = true;
      }

      private void DirectComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
         optionsChanged = true;
      }
   }

   
}
