using Backend.Models;
using Repository.BackendRepository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace Backend.Services.Implement
{
    /*
     * 暫時棄用
     */
    public class BackgroundService
    {
        private readonly IBackendRepository backendRepository;
        private List<string> mode_list = new List<string>() { "insert", "insert_list", "update", "update_list", "delete", "purge_all_insert", "purge" };
        public BackgroundService(IBackendRepository backendRepository)
        {
            this.backendRepository = backendRepository;
        }

        /// <summary>
        /// 加入至背景執行任務(單筆情況)
        /// </summary>
        /// <param name="building_ip"></param>
        /// <param name="building_guid"></param>
        /// <param name="target_table"></param>
        /// <param name="mode"></param>
        /// <param name="parameter"></param>
        /// <param name="fileInfos"></param>
        /// <returns></returns>
        public async Task AddTask(string building_ip = "", string building_guid = "", string target_table = "", string mode = "", Dictionary<string, object> parameter = null, List<Models.FileInfo> fileInfos = null)
        {
            List<string> temp_ips = new List<string>();
            if (!string.IsNullOrEmpty(building_ip))
            {
                //有填寫IP,直接用
                temp_ips.Add(building_ip);
            }
            else if (!string.IsNullOrEmpty(building_guid))
            {
                //未填寫IP,抓取該棟別IP
                var building_where = @"deleted = 0 AND building_guid = @Building_guid";
                var building = await backendRepository.GetOneAsync<BuildInfo>("building", building_where, new { Building_guid = building_guid });

                temp_ips.Add(building.Ip_address_port);
            }
            else
            {
                //未填寫IP、棟別guid 代表全抓
                var building_where = @"deleted = 0";
                var buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", building_where);

                foreach(var build in buildInfos)
                {
                    temp_ips.Add(build.Ip_address_port);
                }
            }

            if(temp_ips != null && temp_ips.Count > 0)
            {
                foreach(var temp_ip in temp_ips)
                {
                    if (!string.IsNullOrEmpty(temp_ip) && !string.IsNullOrEmpty(target_table) && mode_list.Contains(mode.ToLower()))
                    {
                        Dictionary<string, object> backgroundServiceTaskDic = new Dictionary<string, object>()
                        {
                            { "@task_type", 1},
                            { "@target_ip", temp_ip},
                            { "@target_table", target_table},
                            { "@mode", mode.ToLower()},
                            { "@repeat_times", 0},
                            { "@is_complete", 0},
                        };

                        if (parameter != null)
                        {
                            var data_json = JsonSerializer.Serialize(parameter);
                            if (!string.IsNullOrEmpty(data_json))
                            {
                                backgroundServiceTaskDic.Add("@target_data", data_json);
                            }
                        }

                        if (fileInfos != null && fileInfos.Count > 0)
                        {
                            var file_json = JsonSerializer.Serialize(fileInfos);
                            if (!string.IsNullOrEmpty(file_json))
                            {
                                backgroundServiceTaskDic.Add("@target_files", file_json);
                            }
                        }

                        await backendRepository.AddOneByCustomTable(backgroundServiceTaskDic, "background_service_task");
                    }
                }
            }
        }

        /// <summary>
        /// 加入至背景執行任務(多筆情況)
        /// </summary>
        /// <param name="building_ip"></param>
        /// <param name="building_guid"></param>
        /// <param name="target_table"></param>
        /// <param name="mode"></param>
        /// <param name="parameter"></param>
        /// <param name="fileInfos"></param>
        /// <returns></returns>
        public async Task AddTask(string building_ip = "", string building_guid = "", string target_table = "", string mode = "", List<Dictionary<string, object>> parameter = null, List<Models.FileInfo> fileInfos = null)
        {
            List<string> temp_ips = new List<string>();
            if (!string.IsNullOrEmpty(building_ip))
            {
                //有填寫IP,直接用
                temp_ips.Add(building_ip);
            }
            else if (!string.IsNullOrEmpty(building_guid))
            {
                //未填寫IP,抓取該棟別IP
                var building_where = @"deleted = 0 AND building_guid = @Building_guid";
                var building = await backendRepository.GetOneAsync<BuildInfo>("building", building_where, new { Building_guid = building_guid });

                temp_ips.Add(building.Ip_address_port);
            }
            else
            {
                //未填寫IP、棟別guid 代表全抓
                var building_where = @"deleted = 0";
                var buildInfos = await backendRepository.GetAllAsync<BuildInfo>("building", building_where);

                foreach (var build in buildInfos)
                {
                    temp_ips.Add(build.Ip_address_port);
                }
            }

            if (temp_ips != null && temp_ips.Count > 0)
            {
                foreach (var temp_ip in temp_ips)
                {
                    if (!string.IsNullOrEmpty(temp_ip) && !string.IsNullOrEmpty(target_table) && mode_list.Contains(mode.ToLower()))
                    {
                        Dictionary<string, object> backgroundServiceTaskDic = new Dictionary<string, object>()
                        {
                            { "@task_type", 1},
                            { "@target_ip", temp_ip},
                            { "@target_table", target_table},
                            { "@mode", mode.ToLower()},
                            { "@repeat_times", 0},
                            { "@is_complete", 0},
                        };

                        if (parameter != null)
                        {
                            var data_json = JsonSerializer.Serialize(parameter);
                            if (!string.IsNullOrEmpty(data_json))
                            {
                                backgroundServiceTaskDic.Add("@target_data", data_json);
                            }
                        }

                        if (fileInfos != null && fileInfos.Count > 0)
                        {
                            var file_json = JsonSerializer.Serialize(fileInfos);
                            if (!string.IsNullOrEmpty(file_json))
                            {
                                backgroundServiceTaskDic.Add("@target_files", file_json);
                            }
                        }

                        await backendRepository.AddOneByCustomTable(backgroundServiceTaskDic, "background_service_task");
                    }
                }
            }
        }
    }
}