using System;
using System.Collections.Generic;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Configuration;

namespace WK_KLI_LAPS_Service.Controllers.Configuration.SmartChart
{

    public interface ISmartChartService
    {
        IEnumerable<object> GetActiveMasterTables();
        IEnumerable<object> GetActiveFieldAlias();
        IEnumerable<object> GetFormConfigDashboardField(int smartchartconfigid);
        int Add(smartChartModel data);
        int Edit(smartChartModel data);
        object getByID(smartChartModel data);
        IEnumerable<object> GetAllGivenMasterDetails(string tableid);

        int addSmartChartData(string data, string formconfigid, string tablename);
        string getAllSmartChartAnswercount(object data);
        IEnumerable<object> getAllSmartChartAnswerDetails(object data);
        IEnumerable<object> getSmartChartAnswerRenderDetails(object data);
        int updateSmartChartData(string data, string formconfigid, string tablename);
        int deactiveSmartChartAnswerDetails(object data);
        object getEditorDisplayStatusByID(string smartchartconfigid);
        object getAllSmartChartAnswers(object data);
        object getAllSmartChartAnswersCount(object data);

        int updateAllSmartChartData(string data);

    }
    public class SmartChartService : ISmartChartService
    {

        CommonRepository oCommonRepository = new CommonRepository();
        SmartChartRepository oSmartChartRepository = new SmartChartRepository();
        //code to get only active master table name
        public IEnumerable<object> GetActiveMasterTables()
        {
            try
            {
                return oCommonRepository.FindActiveMasterTables();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                throw;
            }

        }

        //code to get only active fields alias name
        public IEnumerable<object> GetActiveFieldAlias()
        {
            try
            {
                return oCommonRepository.FindActiveFieldAlias();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                throw;
            }
        }
        public IEnumerable<object> GetFormConfigDashboardField(int smartchartconfigid)
        {
            try
            {
                return oCommonRepository.GetFormConfigDashboardField(smartchartconfigid);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                throw;
            }

        }

        //code to add form config data
        public int Add(smartChartModel data)
        {
            try
            {
                if (string.IsNullOrEmpty(data.smartchartname))
                    throw new Exception("Value should not be empty");
                int retval = oSmartChartRepository.add(data, data.smartchartname, data.htmldata);
                return retval;
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                throw;
            }
        }

        //code to update data
        public int Edit(smartChartModel data)
        {
            try
            {
                if (string.IsNullOrEmpty(data.smartchartname))
                    throw new Exception("Value should not be empty");
                int retval = oSmartChartRepository.Edit(data, data.smartchartname, data.smartchartconfigid, data.htmldata);
                return retval;
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                throw;
            }
        }

        public object getByID(smartChartModel data)
        {
            return oSmartChartRepository.getByID(data.smartchartconfigid);
        }

        public IEnumerable<object> GetAllGivenMasterDetails(string tableid)
        {
            return oSmartChartRepository.GetAllGivenMasterDetails(tableid);

        }
        //code to add form  data
        public int addSmartChartData(string data, string formconfigid, string tablename)
        {
            int retval = oSmartChartRepository.addSmartChartData(data, formconfigid, tablename);
            return retval;
        }

        public string getAllSmartChartAnswercount(object data)
        {
            return oSmartChartRepository.getAllSmartChartAnswercount(data);
        }

        public IEnumerable<object> getAllSmartChartAnswerDetails(object data)
        {
            try
            {
                return oSmartChartRepository.getAllSmartChartAnswerDetails(data);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);

                throw new Exception(ex.Message);
            }
        }

        public IEnumerable<object> getSmartChartAnswerRenderDetails(object data)
        {
            try
            {
                return oSmartChartRepository.getSmartChartAnswerRenderDetails(data);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);

                throw new Exception(ex.Message);
            }
        }

        public int updateSmartChartData(string data, string formconfigid, string tablename)
        {
            int retval = oSmartChartRepository.updateSmartChartData(data, formconfigid, tablename);
            return retval;
        }
        public int deactiveSmartChartAnswerDetails(object data)
        {
            int retval = oSmartChartRepository.deactiveSmartChartAnswerDetails(data);
            return retval;
        }

        public object getEditorDisplayStatusByID(string smartchartconfigid)
        {
            return oSmartChartRepository.getEditorDisplayStatusByID(Convert.ToInt32(smartchartconfigid));
        }

        public object getAllSmartChartAnswers(object data)
        {
            return oSmartChartRepository.GetAllSmartChartAnswers(data);
        }
        public object getAllSmartChartAnswersCount(object data)
        {
            return oSmartChartRepository.GetAllSmartChartAnswersCount(data);
        }
        public int updateAllSmartChartData(string data)
        {
            int retval = oSmartChartRepository.updateAllSmartChartData(data);
            return retval;
        }

    }
}
