#!/usr/bin/env python3
"""Test the list of possible stages in an object."""

import sys
import json
from pathlib import Path
cwd = Path(__file__).parent.parent
sys.path.insert(0, cwd.as_posix())

from itkdb_gtk import ITkDBlogin, ITkDButils


def set_stages(session, SN):
    """Prepare the stages combo."""
    # Check the object stage
    print("+++ {}".format(SN))
    try:
        obj = ITkDButils.get_DB_component(session, SN)

        with open("object-{}.json".format(SN), "w", encoding="utf-8") as ofile:
            ofile.write(json.dumps(obj, indent=4))

        currentStage = obj["currentStage"]["code"]
        print("Current Stage: {}".format(currentStage))

        for stg in obj["stages"]:
            S = stg["code"]
            print("- {}".format(S))

    except Exception as E:
        print("Something went wrong with the stages\n{}".format(E))


def main():
    """Entry point."""
    dlg = ITkDBlogin.ITkDBlogin()
    client = dlg.get_client()
    if client is None:
        print("Could not connect to DB with provided credentials.")
        dlg.die()
        sys.exit()

    set_stages(client, "IFIC-R5-0046")
    set_stages(client, "20USEM50000110")
    
    set_stages(client, "IFIC-R0-0056")
    set_stages(client, "20USEM00000198")
    
    dlg.die()


if __name__ == "__main__":
    main()
