"""Fix Pop'n Music song display issue in music data.

Revision ID: a522b9e42df4
Revises: 5c927879651b
Create Date: 2019-09-08 19:54:01.499243

"""
from alembic import op
from sqlalchemy.sql import text


# revision identifiers, used by Alembic.
revision = 'a522b9e42df4'
down_revision = '5c927879651b'
branch_labels = None
depends_on = None


def upgrade():
    conn = op.get_bind()

    sql = "SELECT id, name, artist, genre FROM music WHERE game = 'pnm'"
    results = conn.execute(text(sql), {})
    for result in results:
        musicid = result['id']
        new_title = title = result['name']
        new_artist = artist = result['artist']
        new_genre = genre = result['genre']

        # Fix accent issues with title/artist
        accent_lut: Dict[str, str] = {
            "鵝": "7",
            "圄": "à",
            "圉": "ä",
            "鵤": "Ä",
            "鵑": "👁 ",
            "鶤": "©",
            "圈": "é",
            "鵐": "ê",
            "鵙": "Ə",
            "鵲": "ë",
            "！": "!",
            "囿": "♥",
            "鶚": "㊙",
            "鶉": "ó",
            "鶇": "ö",
            "鶲": "Ⓟ",
            "鶫": "²",
            "圍": "@",
            "圖": "ţ",
            "鵺": "Ü",
            "囎": ":",
            "囂": "♡",
            "釁": "🐾",
        }

        for orig, rep in accent_lut.items():
            new_title = new_title.replace(orig, rep)
            new_artist = new_artist.replace(orig, rep)
            new_genre = new_genre.replace(orig, rep)

        if new_title != title or new_artist != artist or new_genre != genre:
            sql = "UPDATE music SET name = :name, artist = :artist, genre = :genre WHERE id = :id AND game = 'pnm'"
            conn.execute(
                text(sql),
                {
                    'id': musicid,
                    'name': new_title,
                    'artist': new_artist,
                    'genre': new_genre,
                },
            )


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###
