using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.web.Services;

namespace wispro.sp.web.Models
{
    public class ReviewerAppealModel
    {
        public PerformanceItem Item { get; set; }

        public AppealType AppealType { get; set; }

        public List<InputField> CreateInputFields { get; set; }
        public List<InputFieldValue> CreateAppealFieldValues { get; set; }

        public List<InputField> inputFields { get; set; }
        public List<InputFieldValue> inputFieldValues { get; set; }

        public List<AttachFile> attachFiles { get; set; }

        public AppealRecord AppealRecord { get; set; }

        public async Task Init(AppealTypeService _atService, int appealRecordId)
        {
            this.AppealRecord =await _atService.getAppealRecord(appealRecordId);
            Item = this.AppealRecord.Item;
            CreateInputFields = await _atService.GetInputFields(this.AppealRecord.TypeId, 0);
            CreateAppealFieldValues = await _atService.GetInputFieldValues(this.AppealRecord.Id,0);
            inputFields = await _atService.GetInputFields(this.AppealRecord.TypeId, 1);
            attachFiles = await _atService.GetAppealRecordAttachFiles(this.AppealRecord.Id);

            inputFieldValues = new List<InputFieldValue>();
            foreach (var field in inputFields)
            {
                if (!string.IsNullOrEmpty(field.MapObjectField) && !string.IsNullOrEmpty(field.MapObjectFieldLabel))
                {
                    List<InputFieldValue> temValues = new List<InputFieldValue>();
                    string[] pList = field.MapObjectField.Split(new char[] { '.' });
                    string[] plList = field.MapObjectFieldLabel.Split(new char[] { '.' });

                    var pInfo = Item.GetType().GetProperty(pList[0]);

                    var o = pInfo.GetValue(Item);


                    if (Array.IndexOf(o.GetType().GetInterfaces(), typeof(IEnumerable)) > -1)
                    {
                        List<string> Values = new List<string>();
                        List<string> Labels = new List<string>();

                        var objList = o as IEnumerable;

                        foreach (var obj in objList)
                        {
                            var objValue = obj;
                            var objLabel = obj;
                            for (int i = 1; i < pList.Length; i++)
                            {
                                objValue = objValue.GetType().GetProperty(pList[i]).GetValue(objValue);

                            }

                            for (int i = 1; i < plList.Length; i++)
                            {
                                objLabel = objLabel.GetType().GetProperty(plList[i]).GetValue(objLabel);
                            }

                            var fValue = new InputFieldValue();
                            fValue.InputField = field;
                            fValue.InputFieldId = field.Id;
                            fValue.Label = objLabel.ToString();
                            if (objValue != null)
                            {
                                fValue.Value = objValue.ToString();
                            }
                            inputFieldValues.Add(fValue);
                            temValues.Add(fValue);
                        }
                    }
                    else
                    {
                        var fValue = new InputFieldValue();
                        fValue.InputField = field;
                        fValue.InputFieldId = field.Id;
                        inputFieldValues.Add(fValue);
                    }
                }
                else
                {
                    var fValue = new InputFieldValue();
                    fValue.InputField = field;
                    fValue.InputFieldId = field.Id;
                    inputFieldValues.Add(fValue);
                }
            }

            
        }

    }
}
