using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TEST_DecodePng
{
    public partial class Form1 : Form
    {
        public string imageFolderPath = "";
        public string selectedFile = "";
        public Form1()
        {
            InitializeComponent();
        }
        public void RefreshUi()
        {
            lsbFiles.Items.Clear();
            if (Directory.Exists(imageFolderPath))
            {
                lsbFiles.Enabled = true;
                string[] files = Directory.GetFiles(imageFolderPath,"*.png");
                foreach (string file in files)
                {
                    lsbFiles.Items.Add(Path.GetFileName(file));
                }

                //TO REMOVE, ONLY HERE FOR DEBUGGING
                files = Directory.GetFiles(imageFolderPath, "*.jpg");
                foreach (string file in files)
                {
                    lsbFiles.Items.Add(Path.GetFileName(file));
                }
            }
            else
            {
                lsbFiles.Enabled = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                imageFolderPath = fbd.SelectedPath;
            }
            else
            {
                //There has been an error trying to browse folder
            }
            RefreshUi();
        }

        private void lsbFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsbFiles.SelectedIndex >= 0)
            {
                string png = imageFolderPath + "\\" + lsbFiles.Items[lsbFiles.SelectedIndex];
                if (File.Exists(png))
                {
                    pbxPng.Image = Image.FromFile(png);
                    selectedFile = png;
                }
            }
        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            PngDecoder decoder = new PngDecoder();
            
            try{
                decoder.Decode(selectedFile);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //decoder.Decode(selectedFile);
        }
    }
}
