﻿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 RS.Common;

namespace RS.PDF.Splitter
{
    public partial class DefaultMetadataForm : Form
    {
        public List<PM.DM.DocumentMetadataModel> Metadata { get; set; }

        public static List<PM.DM.DocumentMetadataModel> ServerMetadatas { get; set; }

        public DefaultMetadataForm()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                ErrorManager.ManageError(ex);
            }
        }
        private void LoadDefaultMetadataForm(object sender, EventArgs e)
        {
            try
            {
                PopulateMetadatas();
            }
            catch (Exception ex)
            {
                ErrorManager.ManageError(ex);
            }
        }

        public static void FillMetadatas()
        {
            if (DefaultMetadataForm.ServerMetadatas == null)
            {
                ServerMetadatas = Program.Consumer.GetMetadatasOfTypeListAndWithDocTypeAssociated("ro").Data;
            }
        }

        private void PopulateMetadatas()
        {
            FillMetadatas();
            if (DefaultMetadataForm.ServerMetadatas != null)
            {
                foreach (var meta in DefaultMetadataForm.ServerMetadatas)
                {
                    ListViewItem listViewItem = new ListViewItem(meta.Name);
                    listViewItem.SubItems.Add(DataConverter.ToString(meta.EntityName));
                    listViewItem.Tag = meta;

                    metasList.Items.Add(listViewItem);
                }
            }

        }

        private void OnClickOKButton(object sender, EventArgs e)
        {
            try
            {
                SelectMetadata();
            }
            catch (Exception ex)
            {
                ErrorManager.ManageError(ex);
            }
        }

        private void SelectMetadata()
        {
            if (metasList.SelectedItems.Count > 0)
            {
                Metadata = new List<PM.DM.DocumentMetadataModel>();
                foreach (ListViewItem listViewItem in metasList.SelectedItems)
                {
                    Metadata.Add(listViewItem.Tag as PM.DM.DocumentMetadataModel);
                }

                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show(Resources.DisplayStrings.PleaseSelectAtLeastOneMeta);
            }
        }

        private void OnDoubleClickSelectMetas(object sender, EventArgs e)
        {
            try
            {
                SelectMetadata();
            }
            catch (Exception ex)
            {
                ErrorManager.ManageError(ex);
            }
        }
    }
}
