import gamesort,variables
from pathlib import Path
from datetime import datetime,timedelta

home = str(Path.home())

def playcard(game,local=False,decay=False):
    cardstring = ""
    if decay == True:
        cardstring += "                  <details class=\"game-item decay\">\n"
    else:
        cardstring += "                  <details class=\"game-item\">\n"
    cardstring += "                    <summary>\n                      <div class=\"platform\">\n                        <div>\n"
    for console in variables.consoles:
        if game["console"] == console["code"]:
            cardstring += "                          <img src=\""
            if local:
                cardstring += variables.localpath + "build/"
            else:
                cardstring += variables.serverpath
            cardstring += console["img"] + ".png\" title=\"" + console["name"] + "\" alt=\"" + console["shortname"] + "\" class=\"bw\">\n"
    cardstring += "                        </div>\n                      </div>\n                      <div class=\"status\">\n"
    try:
        if game["completed"]:
            cardstring += "                        <img src=\""
            if local:
                cardstring += variables.localpath + "build/"
            else:
                cardstring += variables.serverpath
            cardstring += "C.png\" alt=\"C\" title=\"Completed\">\n"
    except:
        try:
            if game["beaten"]:
                cardstring += "                        <img src=\""
                if local:
                    cardstring += variables.localpath + "build/"
                else:
                    cardstring += variables.serverpath
                cardstring += "B.png\" alt=\"B\" title=\"Beaten\">\n"
        except:
            try:
                if game["gameplay"]:
                    if game["id"] in variables.endlessgames:
                        cardstring += "                        <img src=\""
                        if local:
                            cardstring += variables.localpath + "build/"
                        else:
                            cardstring += variables.serverpath
                        cardstring += "E.png\" alt=\"E\" title=\"Endless\">\n"
                    else:
                        cardstring += "                        <img src=\""
                        if local:
                            cardstring += variables.localpath + "build/"
                        else:
                            cardstring += variables.serverpath
                        cardstring += "UF.png\" alt=\"UF\" title=\"Unfinished\">\n"
            except:
                cardstring += "                        <img src=\""
                if local:
                    cardstring += variables.localpath + "build/"
                else:
                    cardstring += variables.serverpath
                cardstring += "UP.png\" alt=\"UP\" title=\"Unplayed\">\n"
    cardstring += "                      </div>\n                      <div class=\"text\">\n                        <div class=\"title\">" + game["name"] + "</div>\n"
    try:
        if len(game["recent"]) > 1:
            cardstring += "                        <div class=\"markdown\">" + game["recent"] + "</div>\n"
    except:
        pass
    try:
        if game["gameplay"]:
            try:
                if game["completed"]:
                    if game["gameplay"] > game["completed"]:
                        if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\""
                            if local:
                                cardstring += variables.localpath + "build/"
                            else:
                                cardstring += variables.serverpath
                            cardstring += "nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
                    else:
                        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
            except:
                try:
                    if game["beaten"]:
                        if game["gameplay"] > game["beaten"]:
                            if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                                cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                            else:
                                cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\""
                                if local:
                                    cardstring += variables.localpath + "build/"
                                else:
                                    cardstring += variables.serverpath
                                cardstring += "nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                except:
                    if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                        if game["id"] in variables.endlessgames:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\""
                            if local:
                                cardstring += variables.localpath + "build/"
                            else:
                                cardstring += variables.serverpath
                            cardstring += "paused.png\" alt=\"⏸\" title=\"Paused\">\n                      </div>\n"
                    else:
                        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\""
                        if local:
                            cardstring += variables.localpath + "build/"
                        else:
                            cardstring += variables.serverpath
                        cardstring += "nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
    except:
        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
    cardstring += "                    </summary>\n                    <div class=\"game-info game-guest\">\n                      <div class=\"data\">\n                        <div class=\"box_2\">\n"
    try:
        if len(game["recent"]) > 1:
            cardstring += "                          <div>\n                            <label>Notes</label>\n                            <div class=\"markdown\">" + game["recent"] + "</div>\n                          </div>\n"
    except:
        pass
    cardstring += "                          <div class=\"flex\">\n                            <div>\n                              <label>Status</label>\n                              <p>"
    try:
        if game["completed"]:
            cardstring += "Completed"
    except:
        try:
            if game["beaten"]:
                cardstring += "Beaten"
        except:
            try:
                if game["gameplay"]:
                    if game["id"] in variables.endlessgames:
                        cardstring += "Endless"
                    else:
                        cardstring += "Unfinished"
            except:
                cardstring += "Unplayed"
    cardstring += "</p>\n                            </div>\n                            <div>\n                              <label>Currently</label>\n                              <p>"
    try:
        if game["gameplay"]:
            try:
                if game["completed"]:
                    if game["gameplay"] > game["completed"]:
                        if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                            cardstring += "Not playing"
                        else:
                            cardstring += "Playing"
                    else:
                        cardstring += "Not playing"
            except:
                try:
                    if game["beaten"]:
                        if game["gameplay"] > game["beaten"]:
                            if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                                cardstring += "Not playing"
                            else:
                                cardstring += "Playing"
                        else:
                            cardstring += "Not playing"
                except:
                    if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                        if game["id"] in variables.endlessgames:
                            cardstring += "Not playing"
                        else:
                            cardstring += "Paused"
                    else:
                        cardstring += "Playing"
    except:
        cardstring += "Not playing"
    cardstring += "</p>\n                            </div>\n                          </div>\n                        </div>\n                        <div class=\"box_1 m-r0\">\n                          <div>\n                            <label>Platform</label>\n                            <p>"
    for console in variables.consoles:
        if game["console"] == console["code"]:
            cardstring += console["name"]
    cardstring += "</p>\n                          </div>\n                          <div>\n                            <label>Last Updated</label>\n                            <p>" + game["lastupdate"].strftime("%Y-%m-%d") + "</p>\n                          </div>\n                        </div>\n                      </div>\n                      <div class=\"history\">\n                        <div class=\"box_1\">\n                          <h2>Milestones</h2>\n                          <div class=\"list\">\n                            <div>\n                              <div>" + game["initialdate"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Added\"></div>Added</div>\n                            </div>\n"
    try:
        if game["firstplayed"]:
            cardstring += "                            <div>\n                              <div>" + game["firstplayed"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot "
            if game["id"] in variables.endlessgames:
                cardstring += "Endless"
            else:
                cardstring += "Started"
            cardstring += "\"></div>Started</div>\n                            </div>\n"
    except:
        pass
    try:
        if game["beaten"]:
            cardstring += "                            <div>\n                              <div>" + game["beaten"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Beaten\"></div>Beaten</div>\n                            </div>\n"
    except:
        pass
    try:
        if game["completed"]:
            cardstring += "                            <div>\n                              <div>" + game["completed"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Completed\"></div>Completed</div>\n                            </div>\n"
    except:
        pass
    cardstring += "                          </div>\n                        </div>\n                      </div>\n                    </div>\n                  </details>\n"
    return cardstring

if __name__ == "__main__":
    playcard(gamesort.games[0],True)