using Hawkeye.VisionBuilder.Workflow.ColorStudioClasses;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Hawkeye.VisionBuilder.Workflow;
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
using Hawkeye.VisionBuilder.Workflow.Operations;
using Hawkeye.VisionBuilder.Workflow.Operations.Filters;
using Hawkeye.VisionBuilder.Workflow.Operations.Memory;


namespace Hawkeye.VisionBuilder.Features.ColorProgileGenerator
{
    public partial class ColorProfileSelection : Form
    {
        private readonly WorkflowList _workflowList;

        public ColorProfileSelection(WorkflowList workflowList)
        {
            _workflowList = workflowList;
            InitializeComponent();
        }

        private void btnProfilePath_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter = "Color Profile File (*.colorprofile)|*.colorprofile";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                btnProfilePath.Text = dialog.FileName;
                LoadProfile(dialog.FileName);
            }
        }

        private void LoadProfile(string dialogFileName)
        {
            var categoryTree = CategoryTree.LoadFromFile(dialogFileName);
            var categories = categoryTree.CategoryItems.Select(c => c.Name).ToList();
            listView1.Items.Clear();
            foreach (var category in categories)
            {
                listView1.Items.Add(category);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var checkedItems= listView1.CheckedItems;

            foreach (ListViewItem item in checkedItems)
            {
                var name = item.Text;
                var operation = new ColorProfileOperation();
                operation.Category.Value = name;
                operation.ColorProfilePath.Path = btnProfilePath.Text;
                _workflowList.Operations.Add(operation);

                var blobFilter = new FindManyBlobsOperation(_workflowList);
                blobFilter.SearchArea = null;
                blobFilter.Label = name;
                _workflowList.Operations.Add(blobFilter);

                var imageFromMemory = new ImageFromMemoryOperation();
                _workflowList.Operations.Add(imageFromMemory);

            }
            Close();

        }
    }
}
