<commit_before>﻿using System.Collections.Generic;
using System.Web.Mvc;
using TeamBins.DataAccess;
using TeamBins.Services;
using TechiesWeb.TeamBins.ViewModels;
using System.Linq;
using System;
using TeamBins.Helpers.Enums;
using TechiesWeb.TeamBins.Infrastructure;
namespace TechiesWeb.TeamBins.Controllers
{

    public class TeamController : BaseController
    {       
       private IssueService issueService;
        public TeamController() {
            issueService=new IssueService(new Repositary(),UserID,TeamID);
        
        }

        public TeamController(IRepositary repositary)
            : base(repositary)
        {            

        }

     
        public ActionResult Index()
        {
            var teams = repo.GetTeams(UserID);
            var teamListVM = new TeamListVM();
            foreach (var team in teams)
            {
                teamListVM.Teams.Add(new TeamVM { ID = team.ID, Name = team.Name , IsTeamOwner=team.CreatedByID==UserID });
            }
            return View(teamListVM);
        }
       
        public ActionResult View(int id)        
        {
            
            var team = repo.GetTeam(id);
            if (team != null)
            {
                var vm = new TeamVM { ID = id, Name = team.Name };

              /*  var teamMembers = team.TeamMembers;
                foreach (var teamMember in teamMembers)
                {
                   var member=new MemberVM { Name = teamMember.User.FirstName + " " + teamMember.User.LastName, EmailAddress=teamMember.User.EmailAddress, JobTitle=teamMember.JobTitle, JoinedDate = teamMember.CreatedDate.ToShortDateString() };
                   member.EmailHash = UserService.GetImageSource(member.EmailAddress);
                   vm.Members.Add(member);
                }*/
       
                return View(vm);
            }
            return View("NotFound");
           
        }/*
        public ActionResult Create()
        {
            return View(new TeamVM());
        }*/
        public ActionResult Add()
        {
            return PartialView("Edit", new TeamVM());
        }
        public ActionResult Edit(int id)
        {
             var team = repo.GetTeam(id);
             if (team != null)
             {
                 var vm = new TeamVM { Name = team.Name, ID = team.ID }; 
                 return PartialView(vm);
             }
             return View("NotFound");
        }

        /*
        public ActionResult AddMember(int id)
        {
            var vm=new AddTeamMemberRequestVM { TeamID=id};
            return View(vm);
        }
        public ActionResult MemberInvited()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddMember(AddTeamMemberRequestVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var team = new TeamMemberRequest { EmailAddress = model.EmailAddress, CreatedByID = UserID };
                    team.TeamID = model.TeamID;
                    team.ActivationCode = model.TeamID + "-" + Guid.NewGuid().ToString("n");
                   /* var result = repo.AddTeamMemberRequest(team);
                    if (result != null)
                    {
                        return RedirectToAction("memberinvited", "Team");
                    }
                    else
                    {
                        //LOG ERROR
                    }*//*
                }
                return View(model);
            }
            catch (Exception ex)
            {
                return View(model);

            }

        }
        */
        [HttpPost]
        public ActionResult Create(TeamVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Team team = new Team { Name = model.Name, ID = model.ID };
                    bool isNew = (model.ID == 0);
                    if (!isNew)
                    {
                        team = repo.GetTeam(model.ID);
                        team.Name = model.Name;                       
                    }
                    else
                    {
                        team.CreatedByID = UserID;
                    }
                    var result = repo.SaveTeam(team);
                    if (result != null)
                    {
                        //If it is a new team, add the user as team member
                        var teamMember = new TeamMember { MemberID = UserID, TeamID = team.ID, CreatedByID = UserID };
                        repo.SaveTeamMember(teamMember);
                        return Json(new { Status = "Success" });
                    }
                }
                return View(model);
            }
            catch(Exception ex)
            {
                log.Error("Error updating team "+model.ID,ex);
            }
            return Json(new { Status = "Error" });
        }
        /*
        public ActionResult Created()
        {
            string strTeamId = TempData["TeamID"].ToString();
            if (!String.IsNullOrEmpty(strTeamId))<commit_msg>Fixed the duplicate team member issue<commit_after>﻿using System.Collections.Generic;
using System.Web.Mvc;
using TeamBins.DataAccess;
using TeamBins.Services;
using TechiesWeb.TeamBins.ViewModels;
using System.Linq;
using System;
using TeamBins.Helpers.Enums;
using TechiesWeb.TeamBins.Infrastructure;
namespace TechiesWeb.TeamBins.Controllers
{

    public class TeamController : BaseController
    {       
       private IssueService issueService;
        public TeamController() {
            issueService=new IssueService(new Repositary(),UserID,TeamID);
        
        }

        public TeamController(IRepositary repositary)
            : base(repositary)
        {            

        }

     
        public ActionResult Index()
        {
            var teams = repo.GetTeams(UserID);
            var teamListVM = new TeamListVM();
            foreach (var team in teams)
            {
                teamListVM.Teams.Add(new TeamVM { ID = team.ID, Name = team.Name , IsTeamOwner=team.CreatedByID==UserID });
            }
            return View(teamListVM);
        }
       
        public ActionResult View(int id)        
        {
            
            var team = repo.GetTeam(id);
            if (team != null)
            {
                var vm = new TeamVM { ID = id, Name = team.Name };

              /*  var teamMembers = team.TeamMembers;
                foreach (var teamMember in teamMembers)
                {
                   var member=new MemberVM { Name = teamMember.User.FirstName + " " + teamMember.User.LastName, EmailAddress=teamMember.User.EmailAddress, JobTitle=teamMember.JobTitle, JoinedDate = teamMember.CreatedDate.ToShortDateString() };
                   member.EmailHash = UserService.GetImageSource(member.EmailAddress);
                   vm.Members.Add(member);
                }*/
       
                return View(vm);
            }
            return View("NotFound");
           
        }/*
        public ActionResult Create()
        {
            return View(new TeamVM());
        }*/
        public ActionResult Add()
        {
            return PartialView("Edit", new TeamVM());
        }
        public ActionResult Edit(int id)
        {
             var team = repo.GetTeam(id);
             if (team != null)
             {
                 var vm = new TeamVM { Name = team.Name, ID = team.ID }; 
                 return PartialView(vm);
             }
             return View("NotFound");
        }

        /*
        public ActionResult AddMember(int id)
        {
            var vm=new AddTeamMemberRequestVM { TeamID=id};
            return View(vm);
        }
        public ActionResult MemberInvited()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddMember(AddTeamMemberRequestVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var team = new TeamMemberRequest { EmailAddress = model.EmailAddress, CreatedByID = UserID };
                    team.TeamID = model.TeamID;
                    team.ActivationCode = model.TeamID + "-" + Guid.NewGuid().ToString("n");
                   /* var result = repo.AddTeamMemberRequest(team);
                    if (result != null)
                    {
                        return RedirectToAction("memberinvited", "Team");
                    }
                    else
                    {
                        //LOG ERROR
                    }*//*
                }
                return View(model);
            }
            catch (Exception ex)
            {
                return View(model);

            }

        }
        */
        [HttpPost]
        public ActionResult Create(TeamVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Team team = new Team { Name = model.Name, ID = model.ID };
                    bool isNew = (model.ID == 0);
                    if (!isNew)
                    {
                        team = repo.GetTeam(model.ID);
                        team.Name = model.Name;                       
                    }
                    else
                    {
                        team.CreatedByID = UserID;
                    }
                    var result = repo.SaveTeam(team);
                    if (result != null)
                    {                        
                        if (isNew)
                        {
                            var teamMember = new TeamMember { MemberID = UserID, TeamID = team.ID, CreatedByID = UserID };
                            repo.SaveTeamMember(teamMember);
                        }
                        return Json(new { Status = "Success" });
                    }
                }
                return View(model);
            }
            catch(Exception ex)
            {
                log.Error("Error updating team "+model.ID,ex);
            }
            return Json(new { Status = "Error" });
        }
        /*
        public ActionResult Created()
        {
            string strTeamId = TempData["TeamID"].ToString();
            if (!String.IsNullOrEmpty(strTeamId))
{'fraction_non_alphanumeric': 0.08812223717086297, 'fraction_numerical': 0.00019219680953296174, 'mean_word_length': 1.1290916530278232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4141623', 'n_tokens_mistral': 2573, 'n_tokens_neox': 2453, 'n_words': 544}