__author__ = 'DarkWeb'

import psycopg2
import traceback
import configparser


def connectDataBase():

    try:

        config = configparser.ConfigParser()
        config.read('../../setup.ini')
        ip = config.get('PostgreSQL', 'ip')
        username = config.get('PostgreSQL', 'username')
        password = config.get('PostgreSQL', 'password')
        database = config.get('PostgreSQL', 'database')

        return psycopg2.connect(host=ip, user=username, password=password, dbname=database)

    except:

        print ("Data base (darkweb_marketplaces) not found.")
        raise SystemExit


def verifyMarketPlace(cur, nameMarket):

    try:

        cur.execute("select market_id from marketPlaces where name_market = %(nameMarket)s limit 1", {'nameMarket': nameMarket})

        recset = cur.fetchall()

        if recset:
            return recset[0][0]
        else:
            return 0

    except:

        trace = traceback.format_exc()
        print (trace)


def verifyVendor(cur, nameVendor, marketId):

    try:

        cur.execute("select vendor_id from vendors where name_vendor = %(nameVendor)s and market_id = %(marketId)s "
                    "limit 1", {'nameVendor': nameVendor, 'marketId': marketId})

        recset = cur.fetchall()

        if recset:
            return recset[0][0]
        else:
            return 0

    except:

        trace = traceback.format_exc()
        print (trace)


def getLastMarketPlace(cur):

    try:

        cur.execute("select market_id from marketPlaces order by market_id desc limit 1")

        recset = cur.fetchall()

        if recset:
            return recset[0][0]
        else:
            return 0

    except:

        trace = traceback.format_exc()
        print (trace)


def getLastVendor(cur):

    try:

        cur.execute("select vendor_id from vendors order by vendor_id desc limit 1")

        recset = cur.fetchall()

        if recset:
            return recset[0][0]
        else:
            return 0

    except:

        trace = traceback.format_exc()
        print (trace)


def create_marketPlace(cur, row, url):

    marketId = verifyMarketPlace(cur, row[0])

    if not marketId:
       marketId = int(getLastMarketPlace(cur) + 1)

       sql = "Insert into marketplaces (market_id, name_market, url_market, dateinserted_market) " \
             "Values (%s, %s, %s, %s)"

       recset = [marketId, row[0], url, row[21]]

       cur.execute(sql, recset)

    return marketId


def create_vendor(cur, row, marketId):

    vendorId = verifyVendor(cur, row[1], marketId)

    if not vendorId:
       vendorId = int(getLastVendor(cur) + 1)

       sql = "Insert into vendors (vendor_id, market_id, name_vendor, rating_vendor, successfultransactions_vendor, dateinserted_vendor) Values (%s, %s, %s, %s, %s, %s)"

       recset = [vendorId, marketId,
                 row[1],
                 row[2] if row[2] != '-1' else None,
                 row[3] if row[3] != '-1' else None,
                 row[21]]

       cur.execute(sql, recset)

    return vendorId


def create_items(cur, row, marketId, vendorId):

    sql = "Insert into items (market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \
          "views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \
          "quantityleft_item, shippedfrom_item, shippedto_item, href_item, lastseen_item, dateinserted_item, " \
          "classification_item) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " \
          "%s, %s, %s)"

    recset = [marketId, vendorId,
              row[4],
              row[5] if row[5] != '-1' else None,
              row[6] if row[6] != '-1' else None,
              row[7] if row[7] != '-1' else None,
              row[8] if row[8] != '-1' else None,
              row[9] if row[9] != '-1' else None,
              row[10] if row[10] != '-1' else None,
              row[11] if row[11] != '-1' else None,
              row[12] if row[12] != '-1' else None,
              row[13] if row[13] != '-1' else None,
              row[14] if row[14] != '-1' else None,
              row[15] if row[15] != '-1' else None,
              row[16] if row[16] != '-1' else None,
              row[17] if row[17] != '-1' else None,
              row[18] if row[18] != '-1' else None,
              row[19] if row[19] != '-1' else None,
              row[20] if row[20] != '-1' else None,
              row[21],
              row[21],
              row[22]]

    cur.execute(sql, recset)


def create_database(cur, con):

    try:

        sql = "create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \
              "url_market character varying(255) null, dateinserted_market timestamp(6) with time zone not null, " \
              "constraint markets_pk primary key (market_id))"
        cur.execute(sql)

        sql = "create table vendors(vendor_id integer not null, market_id integer not null, name_vendor character " \
              "varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor integer " \
              "null, dateinserted_vendor timestamp(6) with time zone not null, constraint vendors_pk primary key (" \
              "vendor_id), constraint vendors_market_id_fkey foreign key (market_id) references marketplaces (" \
              "market_id))"
        cur.execute(sql)

        sql = "create table vendors_history(vendor_id integer not null, market_id integer not null, name_vendor " \
              "character varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor " \
              "integer null, dateinserted_vendor timestamp(6) with time zone not null, constraint vendors_history_pk " \
              "primary key (vendor_id, dateinserted_vendor), constraint vendors_history_vendor_id_fkey foreign key (" \
              "vendor_id) references vendors (vendor_id), constraint vendors_history_market_id_fkey foreign key (" \
              "market_id) references marketplaces (market_id))"
        cur.execute(sql)

        sql = "create table items(market_id integer not null, vendor_id integer not null, name_item character " \
              "varying(255) not null, description_item character varying(1000000) null, cve_item character varying(" \
              "255) null, ms_item character varying(255) null, category_item character varying(255) null, views_item " \
              "integer null, reviews_item integer null, rating_item character varying(255) null, dateadded_item " \
              "character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
              "null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
              "character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
              "varying(255) null, href_item character varying(255) null, lastseen_item timestamp(6) with time zone " \
              "not null, dateinserted_item timestamp(6) with time zone not null, classification_item double " \
              "precision not null, constraint items_pk primary key (market_id, vendor_id, name_item), constraint " \
              "items_market_id_fkey foreign key (market_id) references marketplaces (market_id),constraint " \
              "items_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id))"
        cur.execute(sql)

        sql = "create table items_history(market_id integer not null, vendor_id integer not null, name_item character " \
              "varying(255) not null, description_item character varying(1000000) null, cve_item character varying(" \
              "255) null, ms_item character varying(255) null, category_item character varying(255) null, views_item " \
              "integer null, reviews_item integer null, rating_item character varying(255) null, dateadded_item " \
              "character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
              "null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
              "character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
              "varying(255) null, href_item character varying(255) null, lastseen_item timestamp(6) with time zone " \
              "not null, dateinserted_item timestamp(6) with time zone not null, classification_item double " \
              "precision not null, constraint items_history_pk primary key (market_id, vendor_id, name_item, " \
              "dateinserted_item), constraint items_history_market_id_fkey foreign key (market_id) references " \
              "marketplaces (market_id), constraint items_history_vendor_id_fkey foreign key (vendor_id) references " \
              "vendors (vendor_id))"
        cur.execute(sql)

        con.commit()

    except:

        con.rollback()

        trace = traceback.format_exc()

        if (trace.find("already exists")==-1):
            print ("There was a problem during the database creation." )
            raise SystemExit