import time
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from sqlalchemy.exc import DataError

Mysql_arguments = {
    'user': 'adv_yswg',
    'password': 'HCL1zcUgQesaaXNLbL37O5KhpSAy0c',
    'host': 'rm-wz9yg9bsb2zf01ea4yo.mysql.rds.aliyuncs.com',
    'port': 3306,
    'database': 'selection',
    'charset': 'utf8mb4',
}


def get_country_engine(site_name="us"):
    if site_name == 'us':
        db_ = 'mysql+pymysql://{}:{}@{}:{}/{}?charset={}'.format(*Mysql_arguments.values())
    else:
        Mysql_arguments["database"] = f"selection_{site_name}"
        db_ = 'mysql+pymysql://{}:{}@{}:{}/{}?charset={}'.format(*Mysql_arguments.values())
    engine = create_engine(db_)  # , pool_recycle=3600
    return engine


h6_pg_us = {
    'user': 'postgres',
    'password': 'fazAqRRVV9vDmwDNRNb593ht5TxYVrfTyHJSJ3BS',
    'host': '113.100.143.162',
    'port': 5432,
    'database': 'selection',
    # 'encoding': 'utf-8',
}


def get_pg_country_engine(site_name="us"):
    if site_name == 'us' or site_name == 'mx' or site_name == "ca":
        h6_pg_us["database"] = f"selection"
        db_ = 'postgresql+psycopg2://{}:{}@{}:{}/{}'.format(*h6_pg_us.values())
    else:
        h6_pg_us["database"] = f"selection_{site_name}"
        db_ = 'postgresql+psycopg2://{}:{}@{}:{}/{}'.format(*h6_pg_us.values())
    engine = create_engine(db_, poolclass=NullPool, connect_args={'connect_timeout': 10})  # , pool_recycle=3600
    return engine


def model_split(x):
    if not x:
        return x
    if not x.strip():
        return x
    sym_count = x.split(":")

    if len(sym_count) > 2:
        l = []
        for i in sym_count:
            if sym_count.index(i) in [0, sym_count.index(sym_count[-1])]:
                l.append(i)
                continue
            if i.count("|") > 1:
                if i.rfind("| ") not in (-1, 0):
                    l.append(i[:i.rfind(" ")])
                    l.append(i[i.rfind(" ")::])
                else:
                    l.append(i)
            elif i.count("|") == 1:
                if i.rfind("| ") not in (-1, 0):
                    l.append(i[:i.rfind("| ")])
                    l.append(i[i.rfind("| ")::])
                else:
                    l.append(i)
            else:
                if i.rfind(" ") not in (-1, 0):
                    l.append(i[:i.rfind(" ")])
                    l.append(i[i.rfind(" ")::])
                else:
                    l.append(i)
        new_model_l = []

        for i in [l[i:i+2] for i in range(0, len(l), 2)]:
            new_model_l.append(":".join(i))
        print(x)
        print("|-|".join(new_model_l).replace("||-|", "|-|").replace("|-||", "|-|"))
        return "|-|".join(new_model_l).replace("||-|", "|-|").replace("|-||", "|-|")
    else:
        return x


site = "it"
engine = get_country_engine(site)
pg_engine = get_pg_country_engine(site)

sql1 = f"select * from {site}_asin_comment;"
df_asin_comments = pd.read_sql(sql1, con=engine, chunksize=40000)

for df_asin_comment in df_asin_comments:
    df_asin_comment['new_model'] = df_asin_comment.model.apply(model_split)

# for i in [f'{site}_asin_comment_b00', f'{site}_asin_comment_b01_b06', f'{site}_asin_comment_b07', f'{site}_asin_comment_b08', f'{site}_asin_comment_b09', f'{site}_asin_comment_other']:
#
#     sql1 = f"select * from {i};"
#     df_pg_asin_comments = pd.read_sql(sql1, con=pg_engine, chunksize=40000)
#
#     count = 0
#     for df_asin_comment in df_pg_asin_comments:
#         df_asin_comment['new_model'] = df_asin_comment.model.apply(model_split)
#         del df_asin_comment['model']
#         df_asin_comment.rename(columns={'new_model': 'model'}, inplace=True)
#         # df_asin_comment_s = df_asin_comment[(df_asin_comment.title.str.len() > 255) | (df_asin_comment.comment_url.str.len() > 255) | (df_asin_comment.user_img.str.len() > 255) | (df_asin_comment.user_page.str.len() > 255 )]
#         #
#         # df_asin_comment = df_asin_comment[~(df_asin_comment.title.str.len() > 255) | (df_asin_comment.comment_url.str.len() > 255) | (df_asin_comment.user_img.str.len() > 255) | (df_asin_comment.user_page.str.len() > 255 )]
#         try:
#             df_asin_comment.to_sql(name=f"{i}_copy", con=pg_engine, if_exists='append', index=False)
#             time.sleep(4)
#         except DataError as d_error:
#             df_asin_comment.to_excel(f"./{i}{count}.xlsx")
#         count += 1