using System.Globalization;
using DiunaBI.Domain.Entities;
using DiunaBI.Infrastructure.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Microsoft.Extensions.Configuration;

namespace DiunaBI.Plugins.Morska.Exporters;

public class GoogleSheetExport : MorskaBaseExporter
{
    public override string ExporterType => "GoogleSheet";
    private readonly GoogleDriveHelper _googleDriveHelper;
    private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
    private readonly IConfiguration _configuration;
    public GoogleSheetExport(
        GoogleDriveHelper googleDriveHelper,
        SpreadsheetsResource.ValuesResource googleSheetValues,
        IConfiguration configuration)
    {
        _googleDriveHelper = googleDriveHelper;
        _googleSheetValues = googleSheetValues;
        _configuration = configuration;
    }
    public override void Export(Layer layer)
    {
        if (_googleDriveHelper.Service is null)
        {
            throw new Exception("Google Drive API not initialized");
        }
        try
        {

            var data = new List<IList<object>> { new List<object> { layer.Name! } };

            switch (layer.Type)
            {
                case LayerType.Import:
                    {
                        data.Add(new List<object> { "Code", "Value1" });
                        data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Value1! }));
                        break;
                    }
                case LayerType.Administration:
                    {
                        data.Add(new List<object> { "Code", "Desc1" });
                        data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Desc1! }));
                        break;
                    }
                case LayerType.Processed:
                    {
                        data.Add(new List<object> { "Code", "Value1", "Value2", "Value3", "Value3",
                        "Value5", "Value6", "Value7", "Value8", "Value9", "Value10",
                        "Value11", "Value12", "Value13", "Value14", "Value15", "Value16",
                        "Value17", "Value18", "Value19", "Value20", "Value21", "Value22",
                        "Value23", "Value24", "Value25", "Value26", "Value27", "Value28",
                        "Value29", "Value30", "Value31", "Value32"});

                        data.AddRange(layer.Records!.Select(record => new List<object>
                    {
                        record.Code!,
                        record.Value1!,
                        record.Value2!,
                        record.Value3!,
                        record.Value4!,
                        record.Value5!,
                        record.Value6!,
                        record.Value7!,
                        record.Value8!,
                        record.Value9!,
                        record.Value10!,
                        record.Value11!,
                        record.Value12!,
                        record.Value13!,
                        record.Value14!,
                        record.Value15!,
                        record.Value16!,
                        record.Value17!,
                        record.Value18!,
                        record.Value19!,
                        record.Value20!,
                        record.Value21!,
                        record.Value22!,
                        record.Value23!,
                        record.Value24!,
                        record.Value25!,
                        record.Value26!,
                        record.Value27!,
                        record.Value28!,
                        record.Value29!,
                        record.Value30!,
                        record.Value31!,
                        record.Value32!
                    }));
                        break;
                    }
                default:
                    throw new Exception("Wrong LayerType");
            }

            var body = new Google.Apis.Drive.v3.Data.File
            {
                Name = $"{DateTime.Now.ToString(new CultureInfo("pl-PL"))}",
                MimeType = "application/vnd.google-apps.spreadsheet",
                Parents = new List<string?> { _configuration["exportDirectory"] }
            };
            var request = _googleDriveHelper.Service.Files.Create(body);
            var file = request.Execute();

            var sheetId = file.Id;
            var range = $"Sheet1!A1:AG${data.Count}";

            var valueRange = new ValueRange { Values = data };

            var updateRequest = _googleSheetValues.Update(valueRange, sheetId, range);
            updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
            updateRequest.Execute();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}