using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FotbalPortal.Models;
using FotbalPortal.DAL;

namespace FotbalPortal.Controllers
{
    public class PlayerController : Controller
    {
        private PortalContext db = new PortalContext();

        //
        // GET: /Player/
        [Authorize(Roles = "admin, redaktor, editor, sefredaktor")]
        public ActionResult Index()
        {
            var players = db.Players.Include(p => p.country).Include(p => p.club);
            return View(players.ToList());
        }

        //
        // GET: /Player/Details/5

        public ActionResult Details(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // GET: /Player/Stats/5

        public ActionResult Stats(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }

            List<Result> results = db.Results.Include(r => r.homeClub).Include(r => r.visitorClub)
                .Where(r => r.homeClubID == player.clubID || r.visitorClubID == player.clubID).OrderBy(r => r.round).ToList();

            List<PlayerStats> stats = new List<PlayerStats>();

            foreach (Result result in results) {
                PlayerStats _stats = new PlayerStats
                {
                    playerID = player.id,
                    resultID = result.id,
                    round = result.round,
                    goals = 0,
                    minutes = 0,
                    redCards = 0,
                    yellowCards = 0,
                    homeClub = result.homeClub.name,
                    visitorClub = result.visitorClub.name,
                    result = result.homeClubGoals + ":" + result.visitorClubGoals,
                    date = result.date.ToString("dd.MM.yyyy")
                };

                var list = db.PlayerResults.Where(r => r.resultID == result.id && r.playerID == player.id).ToList();

                if (list.Count > 0)
                {
                    PlayerResult playerResult = list.First();

                    _stats.minutes = playerResult.minutes;
                    _stats.goals = playerResult.goals;
                    _stats.redCards = playerResult.redCards;
                    _stats.yellowCards = playerResult.yellowCards;
                }

                stats.Add(_stats);
            }

            ViewBag.stats = stats;
            ViewBag.playerName = player.firstName + " " + player.lastName;

            return View();
        }

        //
        // GET: /Player/Create
        [Authorize(Roles = "admin, redaktor, editor, sefredaktor")]
        public ActionResult Create()
        {
            ViewBag.countryID = new SelectList(db.Countries, "id", "name");
            ViewBag.clubID = new SelectList(db.Clubs, "id", "name");
            return View();
        }

        //
        // POST: /Player/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Player player)
        {
            if (ModelState.IsValid)
            {
                db.Players.Add(player);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.countryID = new SelectList(db.Countries, "id", "name", player.countryID);
            ViewBag.clubID = new SelectList(db.Clubs, "id", "name", player.clubID);
            return View(player);
        }

        //
        // GET: /Player/Edit/5
        [Authorize(Roles = "admin, redaktor, editor, sefredaktor")]
        public ActionResult Edit(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            ViewBag.countryID = new SelectList(db.Countries, "id", "name", player.countryID);
            ViewBag.clubID = new SelectList(db.Clubs, "id", "name", player.clubID);
            return View(player);
        }

        //
        // POST: /Player/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Player player)
        {
            if (ModelState.IsValid)
            {
                db.Entry(player).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.countryID = new SelectList(db.Countries, "id", "name", player.countryID);
            ViewBag.clubID = new SelectList(db.Clubs, "id", "name", player.clubID);
            return View(player);
        }

        //
        // GET: /Player/Delete/5
        [Authorize(Roles = "admin, redaktor, editor, sefredaktor")]
        public ActionResult Delete(int id = 0)
        {
            Player player = db.Players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // POST: /Player/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Player player = db.Players.Find(id);
            db.Players.Remove(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}