using System;
using System.Windows.Forms;
using Baza_Reklam.Classes.Helpers;
using Baza_Reklam.Classes.Model;
using Baza_Reklam.Classes.Repositories;

namespace Baza_Reklam
{
    public partial class EditAdIssue : Form
    {
        public AdLocation Lokalizacja
        {
            get
            {
                if (cbAdLocation.SelectedItem != null)
                    return (AdLocation)cbAdLocation.SelectedItem;
                return null;
            }
        }

        public NavigationColumn Dzial
        {
            get
            {
                if (cbNavigationColumn.SelectedIndex > 0 && cbNavigationColumn.SelectedItem != null)
                    return (NavigationColumn)cbNavigationColumn.SelectedItem;
                return null;
            }
        }


        public NavigationSubColumn Rozdzial
        {
            get
            {
                if (cbNavigationSubColumn.SelectedIndex > 0 && cbNavigationSubColumn.SelectedItem != null)
                    return (NavigationSubColumn)cbNavigationSubColumn.SelectedItem;
                return null;
            }
        }

        public EditAdIssue(Title title, int? dzialId, int? rozdzialId, int lokalizacjaId)
        {
            InitializeComponent();

            AdLocationRepository adLocationRepository = new AdLocationRepository(ConnString.getConnString().Value);
            ComboBoxHelper.Bind(adLocationRepository.FindAll(), cbAdLocation);
            cbAdLocation.DisplayMember = "Text";
            cbAdLocation.ValueMember = "Id";
            cbAdLocation.SelectedIndex = ComboBoxHelper.GetItemIndex(lokalizacjaId, cbAdLocation);

            if (dzialId.HasValue)
            {
                NavigationColumnRepository navigationColumnRepository =
                    new NavigationColumnRepository(ConnString.getConnString().Value);
                ComboBoxHelper.Bind(navigationColumnRepository.FindAllActiveByTitle(title), cbNavigationColumn);
                cbNavigationColumn.Items.Insert(0, "-- proszę wybrać --");
                cbNavigationColumn.DisplayMember = "Name";
                cbNavigationColumn.ValueMember = "Id";
                cbNavigationColumn.SelectedIndex = 0;

                cbNavigationColumn.SelectedIndex = ComboBoxHelper.GetItemIndex(dzialId.Value, cbNavigationColumn);

                if (rozdzialId.HasValue)
                    cbNavigationSubColumn.SelectedIndex = ComboBoxHelper.GetItemIndex(rozdzialId.Value,
                                                                                      cbNavigationSubColumn);
            }
        }

        private void EditAdIssue_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbNavigationColumn_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(Dzial==null) return;

            NavigationColumnRepository navigationColumnRepository = new NavigationColumnRepository(ConnString.getConnString().Value);
            ComboBoxHelper.Bind(navigationColumnRepository.FindActiveNavigationSubColumnsByNavigationColumn(Dzial), cbNavigationSubColumn);
            cbNavigationSubColumn.Items.Insert(0, "-- proszę wybrać --");
            cbNavigationSubColumn.DisplayMember = "Name";
            cbNavigationSubColumn.ValueMember = "Id";
            cbNavigationSubColumn.SelectedIndex = 0;
        }
    }
}