using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Entity.WorkProcess;
using GxPress.Entity.WorkProcessDto;
using GxPress.EnumConst;


namespace GxPress.Service.Implement
{
    public partial class ProcessService
    {
        public async Task<Process> EditAsync(ProcessDto request)
        {
            request.IconUrl = StringUtils.RemoveDomain(request.IconUrl);
            //1.保存基本信息
            var group = await _processGroupRepository.GetAsync(request.GroupId);
            if (group == null) throw new BusinessException("流程分组不存在");

            var process = _mapper.Map<Process>(request);
            await _processRepository.UpdateAsync(process);

            await _processRequestLimitRepository.DeleteByProcessIdAsync(process.Id);

            //2.保存发起限制
            if (process.LimitType != nameof(LimitTypeConst.NoLimit))
            {
                //TODO 批量插入优化
                foreach (var limit in request.RequestLimits)
                {
                    await _processRequestLimitRepository.InsertAsync(new ProcessRequestLimit
                    {
                        ProcessId = process.Id,
                        DepartmentId = limit.DepartmentId,
                        RoleId = limit.RoleId,
                        UserId = limit.UserId
                    });
                }
            }

            await _processFieldRepository.DeleteByProcessIdAsync(process.Id, request.FormFields);

            var fieldDict = new Dictionary<int, int>();

            //3.保存表单定义
            if (request.FormFields != null)
            {
                foreach (var field in request.FormFields)
                {
                    //获取
                    var formField = await _processFieldRepository.GetFieldAsync(field.Id);
                    if (formField == null)
                    {
                        //保存字段
                        var fieldId = await _processFieldRepository.InsertAsync(new ProcessField
                        {
                            ProcessId = process.Id,
                            Label = field.Label,
                            Placeholder = field.Placeholder,
                            Required = field.Required,
                            Sort = field.Sort,
                            Type = field.Type,
                            Unit = field.Unit,
                            Options = StringUtils.ObjectCollectionToString(field.Options)
                        });
                        fieldDict.Add(field.Id, fieldId);
                    }
                    else
                    {
                        //保存字段
                        var fieldId = await _processFieldRepository.UpdateAsync(new ProcessField
                        {
                            Id = field.Id,
                            ProcessId = process.Id,
                            Label = field.Label,
                            Placeholder = field.Placeholder,
                            Required = field.Required,
                            Sort = field.Sort,
                            Type = field.Type,
                            Unit = field.Unit,
                            Options = StringUtils.ObjectCollectionToString(field.Options)
                        });
                        fieldDict.Add(field.Id, field.Id);
                    }
                }
            }

            //4.保存节点定义

            await _processNodeRepository.DeleteByProcessIdAsync(process.Id);
            if (request.ProcessNodes != null)
            {
                foreach (var node in request.ProcessNodes)
                {
                    await AddNodeAsync(node, 0, process.Id, fieldDict);
                }
            }

            return process;
        }
    }
}