using ETSStariGrad.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Migrations;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ETSStariGrad
{
    public partial class frmStudent : Form
    {

        List<Student> studenti = new List<Student>();

        BindingSource _bsStudneti= new BindingSource();

        public frmStudent(BindingSource bsStudenti)
        {
            InitializeComponent();
            //this._bsStudneti = bsStudenti;
            _bsStudneti.CurrentChanged += _bsStudneti_CurrentChanged;
            STUDENTSKAEF baza=new STUDENTSKAEF();
            _bsStudneti.DataSource=baza.students
                .Select(s=>new Student()
                {
                    ID_STUDENTA=s.ID_STUDENTA,
                    IME=s.IME,
                    PREZIME=s.PREZIME,
                    BROJ=s.BROJ,
                    SMER=s.SMER,
                    GODINA_UPISA=s.GODINA_UPISA
                })
                .ToList();
            lstStudenti.DataSource = _bsStudneti;
            cbStudenti.DataSource = _bsStudneti;
        }

        private void _bsStudneti_CurrentChanged(object sender, EventArgs e)
        {
            bsStudenti_CurrentChanged(sender, e);
        }

        private void btnUnesi_Click(object sender, EventArgs e)
        {

            //Student s = new Student()
            //{
            //    Id= studenti.Count + 1,
            //    Ime = txtIme.Text,
            //    Prezime = txtPrezime.Text,
            //    Indeks = txtIndeks.Text,
            //    Smer = txtSmer.Text
            //};
            //studenti.Add(s);
            //_bsStudneti.Add(s);
            //prikaziStudnete();
            student s=new student()
            {
                IME=txtIme.Text,
                PREZIME=txtPrezime.Text,
                SMER=txtSmer.Text,
                BROJ=short.Parse(txtIndeks.Text
                .Replace($"{txtSmer.Text}-","")
                .Split('-')[0]),
                GODINA_UPISA= txtIndeks.Text.Split('/')[1]
            };
            STUDENTSKAEF baza=new STUDENTSKAEF();
            baza.students.Add(s);
            baza.SaveChanges();
            _bsStudneti.ResetBindings(true);
            isprazniPolja();
        }

        private void prikaziStudnete()
        {
            lstStudenti.Items.Clear();
            lstStudenti.Items.AddRange(studenti.ToArray());
        }

        private void btnUnesi_MouseEnter(object sender, EventArgs e)
        {
            btnUnesi.BackColor = Color.LightGreen;
        }


        private void btnOznaciDugme_MouseEnter(object sender, EventArgs e)
        {
            Button tmp = (Button)sender;
            //var tmp1 = (Button)sender;
            tmp.BackColor = Color.LightGreen;
        }

        private void btnVratiUControl_MouseLeave(object sender, EventArgs e)
        {
            Button tmp = (Button)sender;
            //var tmp1 = (Button)sender;
            tmp.BackColor = Color.Coral;
        }

        private void bsStudenti_CurrentChanged(object sender, EventArgs e)
        {
            var bs = (BindingSource)sender;
            if (bs.Current == null)
            {
                return;
            }
            Student student = (Student)bs.Current;
            txtIme.Text = student.IME;
            txtPrezime.Text = student.PREZIME;
            txtIndeks.Text = $"{student.SMER}-{student.BROJ}/{student.GODINA_UPISA}";
            txtSmer.Text = student.SMER;
            try
            {
                STUDENTSKAEF baza = new STUDENTSKAEF();
                var prosek = baza.zapisniks
                    .Where(z => z.ID_STUDENTA == student.ID_STUDENTA)
                    .Average(z => z.OCENA);
                lblProsek.Text = $"Prosek: {Math.Round(prosek, 2)}";
            }
            catch (Exception ex)
            {
                lblProsek.Text = "Prosek: N/A";
            }
        }

        private void btnNovi_Click(object sender, EventArgs e)
        {
            isprazniPolja();
        }

        private void isprazniPolja()
        {
            txtIme.Text = "";
            txtPrezime.Clear();
            txtIndeks.Text = String.Empty;
            txtSmer.Text = "";
        }

        private void btnIzmeni_Click(object sender, EventArgs e)
        {
            var student=(student)_bsStudneti.Current;
            student.IME = txtIme.Text;
            student.PREZIME = txtPrezime.Text;
            student.SMER = txtSmer.Text;
            student.BROJ = short.Parse(txtIndeks.Text
                .Replace($"{txtSmer.Text}-", "")
                .Split('/')[0]);
            student.GODINA_UPISA = txtIndeks.Text.Split('/')[1];
            STUDENTSKAEF baza = new STUDENTSKAEF();
            baza.students.AddOrUpdate(student);
            baza.SaveChanges();
            _bsStudneti.ResetBindings(true);
        }

        private void btnObrisi_Click(object sender, EventArgs e)
        {
            try
            {
                var student = (student)_bsStudneti.Current;
                STUDENTSKAEF baza = new STUDENTSKAEF();
                baza.students.Remove(student);
                baza.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ne mozete obrisati studenta koji ima prijavljene ispite!");
            }
        }
    }
}
