package converters

import (
	"github.com/mayswind/ezbookkeeping/pkg/converters/alipay"
	"github.com/mayswind/ezbookkeeping/pkg/converters/beancount"
	"github.com/mayswind/ezbookkeeping/pkg/converters/camt"
	"github.com/mayswind/ezbookkeeping/pkg/converters/converter"
	"github.com/mayswind/ezbookkeeping/pkg/converters/custom"
	"github.com/mayswind/ezbookkeeping/pkg/converters/datatable"
	"github.com/mayswind/ezbookkeeping/pkg/converters/default"
	"github.com/mayswind/ezbookkeeping/pkg/converters/feidee"
	"github.com/mayswind/ezbookkeeping/pkg/converters/fireflyIII"
	"github.com/mayswind/ezbookkeeping/pkg/converters/gnucash"
	"github.com/mayswind/ezbookkeeping/pkg/converters/iif"
	"github.com/mayswind/ezbookkeeping/pkg/converters/jdcom"
	"github.com/mayswind/ezbookkeeping/pkg/converters/mt"
	"github.com/mayswind/ezbookkeeping/pkg/converters/ofx"
	"github.com/mayswind/ezbookkeeping/pkg/converters/qif"
	"github.com/mayswind/ezbookkeeping/pkg/converters/wechat"
	"github.com/mayswind/ezbookkeeping/pkg/errs"
	"github.com/mayswind/ezbookkeeping/pkg/models"
)

// GetTransactionDataExporter returns the transaction data exporter according to the file type
func GetTransactionDataExporter(fileType string) converter.TransactionDataExporter {
	if fileType == "csv" {
		return _default.DefaultTransactionDataCSVFileConverter
	} else if fileType == "tsv" {
		return _default.DefaultTransactionDataTSVFileConverter
	} else {
		return nil
	}
}

// GetTransactionDataImporter returns the transaction data importer according to the file type
func GetTransactionDataImporter(fileType string) (converter.TransactionDataImporter, error) {
	if fileType == "ezbookkeeping_csv" {
		return _default.DefaultTransactionDataCSVFileConverter, nil
	} else if fileType == "ezbookkeeping_tsv" {
		return _default.DefaultTransactionDataTSVFileConverter, nil
	} else if fileType == "ezbookkeeping_json" {
		return _default.DefaultTransactionDataJsonFileImporter, nil
	} else if fileType == "ofx" {
		return ofx.OFXTransactionDataImporter, nil
	} else if fileType == "qfx" {
		return ofx.OFXTransactionDataImporter, nil
	} else if fileType == "qif_ymd" {
		return qif.QifYearMonthDayTransactionDataImporter, nil
	} else if fileType == "qif_mdy" {
		return qif.QifMonthDayYearTransactionDataImporter, nil
	} else if fileType == "qif_dmy" {
		return qif.QifDayMonthYearTransactionDataImporter, nil
	} else if fileType == "iif" {
		return iif.IifTransactionDataFileImporter, nil
	} else if fileType == "camt052" {
		return camt.Camt052TransactionDataImporter, nil
	} else if fileType == "camt053" {
		return camt.Camt053TransactionDataImporter, nil
	} else if fileType == "mt940" {
		return mt.MT940TransactionDataFileImporter, nil
	} else if fileType == "gnucash" {
		return gnucash.GnuCashTransactionDataImporter, nil
	} else if fileType == "firefly_iii_csv" {
		return fireflyIII.FireflyIIITransactionDataCsvFileImporter, nil
	} else if fileType == "beancount" {
		return beancount.BeancountTransactionDataImporter, nil
	} else if fileType == "feidee_mymoney_csv" {
		return feidee.FeideeMymoneyAppTransactionDataCsvFileImporter, nil
	} else if fileType == "feidee_mymoney_xls" {
		return feidee.FeideeMymoneyWebTransactionDataXlsFileImporter, nil
	} else if fileType == "feidee_mymoney_elecloud_xlsx" {
		return feidee.FeideeMymoneyElecloudTransactionDataXlsxFileImporter, nil
	} else if fileType == "alipay_app_csv" {
		return alipay.AlipayAppTransactionDataCsvFileImporter, nil
	} else if fileType == "alipay_web_csv" {
		return alipay.AlipayWebTransactionDataCsvFileImporter, nil
	} else if fileType == "wechat_pay_app_xlsx" {
		return wechat.WeChatPayTransactionDataXlsxFileImporter, nil
	} else if fileType == "wechat_pay_app_csv" {
		return wechat.WeChatPayTransactionDataCsvFileImporter, nil
	} else if fileType == "jdcom_finance_app_csv" {
		return jdcom.JDComFinanceTransactionDataCsvFileImporter, nil
	} else {
		return nil, errs.ErrImportFileTypeNotSupported
	}
}

// IsCustomFileFormatFileType returns whether the file type is the custom file format
func IsCustomFileFormatFileType(fileType string) bool {
	return custom.IsDelimiterSeparatedValuesFileType(fileType) || custom.IsCustomExcelFileType(fileType)
}

// CreateNewCustomFileFormatTransactionDataParser returns a new custom transaction data parser according to the file type and encoding
func CreateNewCustomFileFormatTransactionDataParser(fileType string, fileEncoding string) (custom.CustomTransactionDataParser, error) {
	if custom.IsDelimiterSeparatedValuesFileType(fileType) {
		return custom.CreateNewCustomTransactionDataDsvFileParser(fileType, fileEncoding)
	} else if custom.IsCustomExcelFileType(fileType) {
		return custom.CreateNewCustomTransactionDataExcelFileParser(fileType)
	} else {
		return nil, errs.ErrImportFileTypeNotSupported
	}
}

// CreateNewCustomTransactionDataImporter returns a new custom transaction data importer according to the file type and encoding
func CreateNewCustomTransactionDataImporter(fileType string, fileEncoding string, columnIndexMapping map[datatable.TransactionDataTableColumn]int, transactionTypeNameMapping map[string]models.TransactionType, hasHeaderLine bool, timeFormat string, timezoneFormat string, amountDecimalSeparator string, amountDigitGroupingSymbol string, geoLocationSeparator string, geoLocationOrder string, transactionTagSeparator string) (converter.TransactionDataImporter, error) {
	if custom.IsDelimiterSeparatedValuesFileType(fileType) {
		return custom.CreateNewCustomTransactionDataDsvFileImporter(fileType, fileEncoding, columnIndexMapping, transactionTypeNameMapping, hasHeaderLine, timeFormat, timezoneFormat, amountDecimalSeparator, amountDigitGroupingSymbol, geoLocationSeparator, geoLocationOrder, transactionTagSeparator)
	} else if custom.IsCustomExcelFileType(fileType) {
		return custom.CreateNewCustomTransactionDataExcelFileImporter(fileType, columnIndexMapping, transactionTypeNameMapping, hasHeaderLine, timeFormat, timezoneFormat, amountDecimalSeparator, amountDigitGroupingSymbol, geoLocationSeparator, geoLocationOrder, transactionTagSeparator)
	} else {
		return nil, errs.ErrImportFileTypeNotSupported
	}
}
