﻿using Kinare.FormSystem.Repositories.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kinare.FormSystem.Forms;
using Abp.Domain.Entities;

namespace Kinare.FormSystem.Domain.Forms
{
    public class ApplicationFormManager : IApplicationFormManager
    {
        private readonly IApplicationFormRepository applicationFormRepository;
        private readonly IGroupInformationRepository groupInformationRepository;
        private readonly IFieldAttributeRepository fieldAttributeRepository;

        public ApplicationFormManager(IApplicationFormRepository applicationFormRepository, IGroupInformationRepository groupInformationRepository, IFieldAttributeRepository fieldAttributeRepository)
        {
            this.applicationFormRepository = applicationFormRepository;
            this.groupInformationRepository = groupInformationRepository;
            this.fieldAttributeRepository = fieldAttributeRepository;
        }

        public async Task<ApplicationForm> Get(Guid id)
        {
            var entity = await applicationFormRepository.GetAsync(id);

            return entity;
        }

        public async Task<IList<ApplicationForm>> GetAll()
        {
            var entityList = await applicationFormRepository.GetAllListAsync();

            return entityList;
        }

        public async Task<ApplicationForm> Create(ApplicationForm input)
        {
            input = await applicationFormRepository.InsertAsync(input);

            return null;
        }

        public async Task<ApplicationForm> Update(ApplicationForm input)
        {
            var groupInformationList = input.GroupInformationList;
            input.GroupInformationList = null;

            for (var i = 0; i < groupInformationList.Count; i++)
            {
                var groupInformation = groupInformationList.ElementAt(i);
                var fieldAttributeList = groupInformation.FieldAttributeList;

                if (groupInformation.IsTransient())
                {
                    groupInformation.ApplicationFormId = input.Id;
                }

                groupInformation.FieldAttributeList = null;
                groupInformation = await groupInformationRepository.InsertOrUpdateAsync(groupInformation);

                for (int j = 0; j < fieldAttributeList.Count; j++)
                {
                    var fieldAttribute = fieldAttributeList.ElementAt(j);

                    if (fieldAttribute.IsTransient())
                    {
                        fieldAttribute.GroupInformationId = groupInformation.Id;
                    }


                    fieldAttribute = await fieldAttributeRepository.InsertOrUpdateAsync(fieldAttribute);
                }

                groupInformation.FieldAttributeList = fieldAttributeList;
            }

            input = await applicationFormRepository.InsertOrUpdateAsync(input);
            input.GroupInformationList = groupInformationList;

            return input;
        }

        public async Task Delete(ApplicationForm input)
        {
            input.IsDeleted = false;

            input = await applicationFormRepository.UpdateAsync(input);
        }
    }
}
