"""
MIT License

scrapy_mysql_pipeline: Asynchronous mysql Scrapy item pipeline

Copyright (c) 2017 Iaroslav Russkykh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import logging
import pprint

from sqlalchemy import create_engine
from twisted.internet import defer

logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')


class MySQLPipeline(object):  #
    """
    Defaults:
    MYSQL_HOST = 'localhost'
    MYSQL_PORT = 3306
    MYSQL_USER = None
    MYSQL_PASSWORD = ''
    MYSQL_DB = None
    MYSQL_TABLE = None
    MYSQL_UPSERT = False
    MYSQL_RETRIES = 3
    MYSQL_CLOSE_ON_ERROR = True
    MYSQL_CHARSET = 'utf8'
    Pipeline:
    ITEM_PIPELINES = {
       'scrapy_mysql_pipeline.MySQLPipeline': 300,
    }
    """
    stats_name = 'mysql_pipeline'

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler)

    def __init__(self, crawler):
        self.stats = crawler.stats
        self.settings = crawler.settings
        db_args = {
            'driver': self.settings.get('DB_DRIVER', 'mysql'),
            'host': self.settings.get('MYSQL_HOST', 'localhost'),
            'port': self.settings.get('MYSQL_PORT', 3306),
            'user': self.settings.get('MYSQL_USER', None),
            'password': self.settings.get('MYSQL_PASSWORD', ''),
            'db': self.settings.get('MYSQL_DB', None),
            # 'charset': self.settings.get('MYSQL_CHARSET', 'utf8'),
            # 'cursorclass': DictCursor,
            # 'cp_reconnect': True,
        }
        self.retries = self.settings.get('MYSQL_RETRIES', 3)
        self.close_on_error = self.settings.get('MYSQL_CLOSE_ON_ERROR', True)
        self.upsert = self.settings.get('MYSQL_UPSERT', False)
        table_specifier = self.settings.get('MYSQL_TABLE', None)
        if table_specifier is not None:
            if '%' in table_specifier:
                # Allow spider's name to be substituted using sprintf
                self.get_table = lambda: table_specifier % self.spider.name
            else:
                # Table name is the same for all spiders
                self.get_table = lambda: table_specifier
        else:
            # Default is to use the spider's name as the table name
            self.get_table = lambda: self.spider.name

        # self.db = adbapi.ConnectionPool('pymysql', **db_args)

        self.db = create_engine(
            "{driver}://{username}:{password}@{ipaddress}:{port}/{dbname}".format(
                driver=db_args['driver'],
                username=db_args['user'],
                password=db_args['password'],
                ipaddress=db_args['host'],
                port=db_args['port'],
                dbname=db_args['db']
            ),
            # pool_size=dbconf.CONNECTION_POOL_SIZE,
        )

    def close_spider(self, spider):
        self.db.close()

    def open_spider(self, spider):
        self.spider = spider

    @property
    def table(self):
        return self.get_table()

    @staticmethod
    def preprocess_item(item):
        """Can be useful with extremly straight-line spiders design without item loaders or items at all
        CAVEAT: On my opinion if you want to write something here - you must read
        http://scrapy.readthedocs.io/en/latest/topics/loaders.html before
        """
        return item

    def postprocess_item(self, *args):
        """Can be useful if you need to update query tables depends of mysql query result"""
        pass

    @defer.inlineCallbacks
    def process_item(self, item, spider):
        retries = self.retries
        status = False
        while retries:
            try:
                item = self.preprocess_item(item)
                yield self.db.runInteraction(self._process_item, item)
            # except OperationalError as e:
            #     if e.args[0] in (
            #             CR_SERVER_GONE_ERROR,
            #             CR_SERVER_LOST,
            #             CR_CONNECTION_ERROR,
            #     ):
            #         retries -= 1
            #         logger.info('%s %s attempts to reconnect left', e, retries)
            #         self.stats.inc_value('{}/reconnects'.format(self.stats_name))
            #         continue
            #     logger.exception('%s', pprint.pformat(item))
            #     self.stats.inc_value('{}/errors'.format(self.stats_name))
            except Exception:
                logger.exception('%s', pprint.pformat(item))
                self.stats.inc_value('{}/errors'.format(self.stats_name))
            else:
                status = True  # executed without errors
            break
        else:
            if self.close_on_error:  # Close spider if connection error happened and MYSQL_CLOSE_ON_ERROR = True
                spider.crawler.engine.close_spider(spider, '{}_fatal_error'.format(self.stats_name))
        self.postprocess_item(item, status)
        yield item

    def _generate_sql(self, data):
        columns = lambda d: ', '.join(['`{}`'.format(k) for k in d])
        values = lambda d: [v for v in d.values()]
        placeholders = lambda d: ', '.join(['%s'] * len(d))
        if self.upsert:
            sql_template = 'INSERT INTO `{}` ( {} ) VALUES ( {} ) ON DUPLICATE KEY UPDATE {}'
            on_duplicate_placeholders = lambda d: ', '.join(['`{}` = %s'.format(k) for k in d])
            return (
                sql_template.format(
                    self.table, columns(data),
                    placeholders(data), on_duplicate_placeholders(data)
                ),
                values(data) + values(data)
            )
        else:
            sql_template = 'INSERT INTO `{}` ( {} ) VALUES ( {} )'
            return (
                sql_template.format(self.table, columns(data), placeholders(data)),
                values(data)
            )

    def _process_item(self, tx, row):
        sql, data = self._generate_sql(row)
        try:
            tx.execute(sql, data)
        except Exception:
            logger.error("SQL: %s", sql)
            raise
        self.stats.inc_value('{}/saved'.format(self.stats_name))
