# -*- coding: utf-8 -*-

import csv
import sys

from PySide6.QtCore import QStandardPaths
from PySide6.QtGui import QImage, QPainter

from OpenNumismat.Collection.Import import _Import
from OpenNumismat.Settings import Settings
from OpenNumismat.Tools.CachedPoolManager import CachedPoolManager

IMAGE_CONNECTION_TIMEOUT = 30


class ImportCoinSnap(_Import):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.http = CachedPoolManager(parent)

    @staticmethod
    def isAvailable():
        return True

    @staticmethod
    def defaultDir():
        dirs = QStandardPaths.standardLocations(QStandardPaths.DownloadLocation)
        if dirs:
            return dirs[0]
        else:
            return ''

    def _connect(self, src):
        return src

    def _getRows(self, srcFile):
        maxInt = sys.maxsize
        while True:
            # decrease the maxInt value by factor 10
            # as long as the OverflowError occurs.
            try:
                csv.field_size_limit(maxInt)
                break
            except OverflowError:
                maxInt = maxInt // 10

        rows = []
        with open(srcFile, 'r', encoding='utf-8', errors='ignore') as f:
            first_line = True
            reader = csv.reader(f, delimiter=',')
            for row in reader:
                if first_line:
                    first_line = False
                else:
                    rows.append(row)

        return rows

    def _setRecord(self, record, row):
        record.setValue('status', 'owned')
        try:
            record.setValue('country', row[1])
            if ' ' in row[2]:
                value, unit = row[2].split(' ', 1)
            else:
                value = ''
                unit = row[2]
#            if '.' in value:
#                value = value.replace('.', '')
            record.setValue('value', value)
            record.setValue('unit', unit)
            record.setValue('year', row[5])
            mintmark = row[6]
            if mintmark in (self.tr("No mint mark"), "No mint mark",
                            "Sem marca da casa da moeda", "Kein Münzzeichen"):
                mintmark = None
            record.setValue('mintmark', mintmark)
            subjectshort = row[3]
            if subjectshort in (self.tr("Common series"), "Common series",
                                "Séries comuns", "Gemeinsame Serie"):
                subjectshort = None
            record.setValue('subjectshort', subjectshort)
            record.setValue('price3', row[8])
            record.setValue('catalognum1', row[4])
            record.setValue('payprice', row[11])
            record.setValue('grade', row[7])
            record.setValue('material', row[9])
            record.setValue('features', row[16])

            record.setValue('obverseimg', self.__getImage(row[12]))
            record.setValue('reverseimg', self.__getImage(row[13]))
        except IndexError:
            pass

        record.setValue('title', self.__generateTitle(record))

    def __getImage(self, url):
        if url:
            response_data = self.http.get(url, timeout=IMAGE_CONNECTION_TIMEOUT)
            if not response_data:
                return None

            image = QImage()
            if image.loadFromData(response_data):
                return self.__fixTransparentImage(image)

        return None

    def __fixTransparentImage(self, image):
        if image.hasAlphaChannel() and not Settings()['transparent_store']:
            # Fill transparent color if present
            color = Settings()['transparent_color']
            fixedImage = QImage(image.size(), QImage.Format_RGB32)
            fixedImage.fill(color)
            painter = QPainter(fixedImage)
            painter.drawImage(0, 0, image)
            painter.end()
        else:
            fixedImage = image

        return fixedImage

    def __generateTitle(self, record):
        title = ""
        if record.value('country'):
            title += record.value('country') + ' '
        if record.value('value'):
            title += record.value('value') + ' '
        if record.value('unit'):
            title += record.value('unit') + ' '
        if record.value('year'):
            title += record.value('year') + ' '
        if record.value('subjectshort'):
            title += '(' + record.value('subjectshort') + ') '
        if record.value('variety'):
            title += '/' + record.value('variety') + '/'

        return title.strip()
