using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.entity.workflowDefine;
using wispro.sp.share.Utility;

namespace wispro.sp.api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConditinController : ControllerBase
    {
        spDbContext Context;
        public ConditinController(spDbContext context)
        {
            Context = context;
        }

        
        
        private List<Staff> GetStaffList(UserField userField,object dynBindObject,entity.workflowInstance.WorkflowInstance Instance)
        {
            switch (userField.UserConditionType)
            {
                case UserConditionType.Department:
                    try
                    {
                        var deptId = int.Parse(userField.Department);
                        return Context.Staffs.Where<Staff>(s =>
                            (s.Positions.Where<DepartmentPosition>(p => p.departmentId == deptId).Count() > 0))
                            .ToList<Staff>();
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                    
                    break;
                case UserConditionType.DepartmentPosition:
                    try {
                        var deptId = int.Parse(userField.Department);
                        var pId = int.Parse(userField.Positon);

                        return Context.Staffs.Where<Staff>(s =>
                            (s.Positions.Where<DepartmentPosition>(p => p.departmentId == deptId && p.PositionId == pId).Count() > 0))
                            .ToList<Staff>();
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException("没有找到指定的部门或职位");
                    }
                    break;
                    
                case UserConditionType.Staff:
                    return new List<Staff>{ GetUser(userField, dynBindObject)};
                    break;
                case UserConditionType.UserDepartment:
                    Staff temUser =  GetUser(userField, dynBindObject);

                    if (temUser != null)
                    {
                        var temDeptPosition = Context.DepartmentPositions.Where<DepartmentPosition>(d =>
                            d.departmentId == int.Parse(userField.Department)
                            && d.StaffId == temUser.Id).FirstOrDefault();
                        var temDeptId = temDeptPosition.departmentId;
                        var retList = Context.DepartmentPositions.Where<DepartmentPosition>(dp => 
                            dp.departmentId == temDeptId)
                            .Select(s => s.Staff)
                            .ToList();

                        return retList;
                    }
                    else
                    {
                        throw new ApplicationException("指定的员工不存在");
                    }
                    break;
                case UserConditionType.UserDepartmentPosition:
                    Staff temUser1 = GetUser(userField, dynBindObject);

                    if (temUser1 != null)
                    {
                        var temDeptPosition = Context.DepartmentPositions.Where<DepartmentPosition>(d =>
                            d.departmentId == int.Parse(userField.Department)
                            && d.StaffId == temUser1.Id).FirstOrDefault();
                        var temDeptId = temDeptPosition.departmentId;
                        var retList = Context.DepartmentPositions.Where<DepartmentPosition>(dp => 
                            dp.departmentId == temDeptId && 
                            dp.PositionId == int.Parse(userField.Positon))
                            .Select(s => s.Staff)
                            .ToList();

                        return retList;
                    }
                    else
                    {
                        throw new ApplicationException("指定的员工不存在");
                    }
                    break;
            }

            return new List<Staff>();
        }

        private Staff GetUser(UserField userField, object dynBindObject)
        {
            Staff temUser = null;
            switch (userField.UserType)
            {
                case UserType.BindObjectProperty:
                    try
                    {
                        temUser = share.Utility.ObjectHelper.GetPropertyValue(userField.UserValue, dynBindObject) as Staff;

                    }
                    catch
                    {
                        throw new ApplicationException("指定的属性属性不是人员类型");
                    }
                    break;
                case UserType.DoActionUser:

                    break;
                case UserType.LoginUser:
                    temUser = Context.Staffs.Where<Staff>(s => s.Name == User.Identity.Name).FirstOrDefault();
                    break;
                case UserType.Staff:
                    temUser = Context.Staffs.Where<Staff>(s => s.Id == int.Parse(userField.UserValue)).FirstOrDefault();
                    break;
            }

            return temUser;
        }
    }
}
