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

namespace SanSystem
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmStrassenList : Form
    {
        BindingSource bs = new BindingSource();
        /// <summary>
        /// 
        /// </summary>
        public frmStrassenList()
        {
            InitializeComponent();
        }

        private void buildStreetList()
        {
            lst_strassen.DataSource = null;
            List<string> strassen = Datenbank.Instance.loadedProjekt.Objekte.Select(x => x.StrasseName).Distinct().ToList();
            //bs.DataSource = strassen;
            lst_strassen.DataSource = strassen;
        }

        private void frmStrassenList_Load(object sender, EventArgs e)
        {
            this.Height = this.MdiParent.Height - 120;
            buildStreetList();
            btn_add.Text = Global.Instance.language.Labels["add_objekt"];
        }
        

        private void btn_add_Click(object sender, EventArgs e)
        {
            KlassenBIB.Inspektionsobjekt inspektionsobjekt = new KlassenBIB.Inspektionsobjekt();
            Datenbank.Instance.loadedProjekt.Objekte.Add(inspektionsobjekt);
            frmObjektEdit frmObjektEdit = new frmObjektEdit(inspektionsobjekt);
            frmObjektEdit.MdiParent = this.MdiParent;
            frmObjektEdit.FormClosed += FrmObjekt_FormClosed;
            frmObjektEdit.Show();            
        }

        private void lst_strassen_DoubleClick(object sender, EventArgs e)
        {
            if ((sender as ListBox).SelectedItem == null) return;
            string selectedStreetName = (sender as ListBox).SelectedItem.ToString();
            if (selectedStreetName == null) return;

            frmObjekteList frmObjekteList = new frmObjekteList(selectedStreetName);
            frmObjekteList.MdiParent = this.MdiParent;
            frmObjekteList.FormClosed += FrmObjekt_FormClosed;
            frmObjekteList.Show();
        }

        private void FrmObjekt_FormClosed(object sender, FormClosedEventArgs e)
        {
            buildStreetList();
        }
    }

}
