using AntDesign;
using AntDesign.TableModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;
using wispro.sp.share;

namespace wispro.sp.web.Services
{
    public class OrganizationService
    {
        private readonly IHttpService _httpClient;
        

        public OrganizationService(IHttpService httpClient)
        {
            _httpClient = httpClient;
           
        }
        public async Task<Department> SaveDept(Department dept)
        {
            var data = await _httpClient.Post<Department>($"Organization/SaveDept",dept);
            return data;
        }

        public async Task<List<Department>> GetDepartments()
        {
            var data = await _httpClient.Get<List<Department>>($"Organization/GetDepartments");
            return data;
        }

        public async Task<List<Position>> getPositions(int? deptId)
        {
            var ret = await _httpClient.Get<List<Position>>($"Organization/GetPositions?deptId={deptId}");
            return ret;
        }

        public async Task<List<Customer>> GetAllCustomer()
        {
            var ret = await _httpClient.Get<List<Customer>>($"Organization/GetCustomers");
            return ret;
        }

        public async  Task<bool> DeleteDept(int deptId)
        {
            var ret =  await _httpClient.Get<bool>($"Organization/DeleteDept?Id={deptId}");
            return ret;
           
        }

        public async Task<ListApiResponse<Staff>> GetStaffs(Department dept,int _pageIndex,int _pageSize)
        {

            if (dept == null || dept.Id <= 0)
            {
                ListApiResponse<Staff> data = await _httpClient.Get<ListApiResponse<Staff>>($"Staff/Query?pageIndex={_pageIndex}&pageSize={_pageSize}");
                return data;
            }
            else
            {
                ListApiResponse<Staff> data = await _httpClient.Get<ListApiResponse<Staff>>($"Staff/QueryInDepartment?deptId={dept.Id}&pageIndex={_pageIndex}&pageSize={_pageSize}");
                return data;
            }

        }

        string GetRandomuserParams(QueryModel<Staff> queryModel)
        {
            List<string> query = new List<string>()
        {
                $"pageSize={queryModel.PageSize}",
                $"pageIndex={queryModel.PageIndex}",
            };

            queryModel.SortModel.ForEach(x =>
            {
                if (x.Sort != null)
                {
                    query.Add($"sortField={x.FieldName.ToLower()}");
                    query.Add($"sortOrder={x.Sort}");
                }
            });

            queryModel.FilterModel.ForEach(filter =>
            {
                filter.SelectedValues.ForEach(value =>
                {
                    query.Add($"{filter.FieldName.ToLower()}={value}");
                });
            });

            return string.Join('&', query);
        }

        public async Task<ListApiResponse<Staff>> GetStaffs(Department dept, QueryModel<Staff> queryModel)
        {
            ListApiResponse<Staff> data = await _httpClient.Get<ListApiResponse<Staff>>($"Staff/Query?DeptId={dept.Id}&" + GetRandomuserParams(queryModel));
            return data;

        }

        public async Task<List<wispro.sp.entity.StaffGrade>> GetStaffGrades()
        {
            var data = await _httpClient.Get<List<wispro.sp.entity.StaffGrade>>($"StaffGrade/GetAll");
            return data;
        }

        public async Task<ApiSaveResponse> SaveUser(Department dept,Position position,Staff staff)
        {
            SaveUserObject saveUser = new SaveUserObject();
            saveUser.deptId = dept.Id;
            saveUser.positionId = position.Id;
            saveUser.staff = staff;
            var data =await _httpClient.Post<ApiSaveResponse>("Staff/SaveUser", saveUser);

            return data;
        }

        public async Task Init()
        {
             await _httpClient.Get<ApiSaveResponse>("Staff/Init");
        }

        public async Task<Position> getPosition(Staff staff, Department editDepartment)
        {
            try
            {
                var data = await _httpClient.Get<Position>($"Organization/GetPosition?deptId={editDepartment.Id}&staffId={staff.Id}");
                return data;
            }
            catch
            {
                return null;
            }
        }
    }

}
