#####################################################################
#                SHOP / TRANSFER
#####################################################################

screen item_shop_scr(*args):
    $ itemsL = args[0] # shop or npc items
    $ headerL = args[1]
    $ itemsR = args[2] # mc items
    $ headerR = args[3]
    $ spread = args[4]
    $ mode = args[5] 
    
    image "05_gui/mask_shop_ohweemod.png"
    text C_HI_B+"{}".format(headerL)+C_HI_E size FSIZE_LARGE pos 800, 85 xanchor 0.5
    #bar xsize 3 ysize 750 pos 1150, 150
    text C_HI_B+"{}".format(headerR)+C_HI_E size FSIZE_LARGE pos 1485, 85 xanchor 0.5

    if mode == "buy":
        text "Credit Line: {:,} Cr".format(GAME.money) pos 40, 95 

    #####################################################################
    #                EXIT BUTTON
    #####################################################################
    if mode == "multi":
        frame xsize 350 ysize 64 pos 0,900 background Solid("#00000000") padding 0,0:
            image "05_gui/gui_menu_option.png" yalign 0.5
            textbutton "Execute Transfer" yalign 0.5 action Return(False)xpos 20
            button action Return(False)xpos 20
            key "c" action Return (False)
            key "C" action Return (False)
    elif mode == "drone":
        frame xsize 350 ysize 64 pos 0,900 background Solid("#00000000") padding 0,0:
            image "05_gui/gui_menu_option.png" yalign 0.5
            textbutton "Activate Drone" yalign 0.5 action Return(False)xpos 20
            button action Return(False)xpos 20
            key "a" action Return (False)
            key "A" action Return (False)
            key "K_SPACE" action Return (False)
            key "x" action Return (False)
            key "X" action Return (False)
    else:
        frame xsize 350 ysize 64 pos 0,900 background Solid("#00000000") padding 0,0:
            image "05_gui/gui_menu_option.png" yalign 0.5
            button action Return(False)xpos 20
            textbutton "Exit (X)" yalign 0.5 action Return(False)xpos 20
            key "x" action Return (False)
            key "X" action Return (False)


    #####################################################################
    #                SHOP
    #####################################################################

    grid 5 7 pos 510,155 spacing 10:
        $ n = 0
        $ shown = []
        $ itemsL.sort(key = lambda x: (itemsort.index(x.kind), x.name))
        for i in itemsL:
            if i not in shown and n < 35:
                $ shown.append(i)
                $ n += 1
                frame xsize 110 ysize 110 background Solid("#0099cc55"):
                    #image "ico bg7.png" zoom 0.10 yalign 0.5 xalign 0.5
                    image "08_items/ico {}.png".format(i.ID) zoom 0.25 yalign 0.5 xalign 0.5
                    if itemsL.count(i)>1:
                        text "{}x".format(itemsL.count(i))
                    button action Return(["buy", i]) tooltip i
        for j in range(35-n):
            frame xsize 110 ysize 110 background Solid("#00336655")
            #image "ico bg7.png" zoom 0.10 yalign 0.5 xalign 0.5

    #####################################################################
    #                YOUR ITEMS
    #####################################################################

    side "c r":
        $ C_COL, C_ROW = 5,11
        area 1190,155,650, 830
        #viewport id "vp":
            #draggable True
            #mousewheel True
            #yadjustment yadj

        vpgrid id "vpg":# C_COL C_ROW spacing 10:
                cols 5
                spacing 10
                draggable True
                mousewheel True
                yadjustment yadj
                $ n = 0
                $ shown = []
                for i in itemsR:
                    if i not in shown and i.name != "Drugs": #and n < 50 and "Drugs" not in i:
                        $ shown.append(i)
                        $ n += 1
                        frame xsize 110 ysize 110 background Solid("#0099cc55"):
                            #image "ico bg7.png" zoom 0.125 yalign 0.5 xalign 0.5
                            image "08_items/ico {}.png".format(i.ID) zoom 0.25 yalign 0.5 xalign 0.5
                            if itemsR.count(i)>1:
                                text "{}x".format(itemsR.count(i))
                            button action Return(["sell", i]) tooltip i
                #for j in range(C_COL*C_ROW-n):
                if n%5 > 0:
                    for j in range (5 - n%5):
                        frame xsize 110 ysize 110 background Solid("#00336655")
                        
                if headerL == "SHADY TRADER":        
                  text C_HI_B+"Non-\n returnable\n    items:"+C_HI_E size 20 ypos 50 bold True
                  for j in range(4):
                    null
                        #frame xsize 110 ysize 110 background Solid("#0099cc55")
                  $ n1 = 0
                  for k in GAME.ship.cargo:
                    if k not in shown and "Drugs" in k.name:
                        $ shown.append(k)
                        $ n1 += 2
                        frame xsize 110 ysize 110 background Solid("#0099cc55"):
                            image "08_items/ico {}.png".format(k.ID) zoom 0.25 yalign 0.5 xalign 0.5
                            text "{}x".format(GAME.ship.cargo.count(k))
                            button action NullAction() tooltip k
                        frame xsize 110 ysize 110 background Solid("#00336655")
                  for k in GAME.ship.cargo:
                    if k not in shown and k.kind == "pod":
                        $ n1 += 1
                        $ shown.append(k)
                        frame xsize 110 ysize 110 background Solid("#0099cc55"):
                            image "08_items/ico {}.png".format(k.ID) zoom 0.25 yalign 0.5 xalign 0.5
                            text "{}x".format(GAME.ship.cargo.count(k))
                            button action NullAction() tooltip k
                  if n > 0:
                    for j1 in range(5-n1):
                      frame xsize 110 ysize 110 background Solid("#00336655")
                    
                  $n2 = 0
                  for k in GAME.ship.cargo:
                    if k not in shown and "Bot Crate" in k.name:
                        $ n2 += 1
                        $ shown.append(k)
                        frame xsize 110 ysize 110 background Solid("#0099cc55"):
                            image "08_items/ico {}.png".format(k.ID) zoom 0.25 yalign 0.5 xalign 0.5
                            text "{}x".format(GAME.ship.cargo.count(k))
                            button action NullAction() tooltip k
                  if n2 > 0:
                    for j1 in range(5-n2):
                      frame xsize 110 ysize 110 background Solid("#00336655")
                    
                  $n3 = 0
                  for k in GAME.ship.cargo:
                    if k not in shown and k.kind == "subunit":
                        if "AI Core" not in k.name:
                            $ n3 += 1
                            $ shown.append(k)
                            frame xsize 110 ysize 110 background Solid("#0099cc55"):
                                image "08_items/ico {}.png".format(k.ID) zoom 0.25 yalign 0.5 xalign 0.5
                                text "{}x".format(GAME.ship.cargo.count(k))
                                button action NullAction() tooltip k
                  if n3 > 0:
                    for j1 in range(5-n3):
                      frame xsize 110 ysize 110 background Solid("#00336655")
                            
                        

        vbar value YScrollValue("vpg")

    $ tooltip = GetTooltip()
    if tooltip and isinstance(tooltip,Item):
        frame padding 20,20 xsize 560 ysize 920 pos 35, 210 background Solid("#39210000"):
            # image "ico bg7.png" zoom 0.25 xalign 0.5
            image "08_items/ico {}.png".format(tooltip.ID) zoom 0.75
            vbox xsize 430 ypos 380 xpos -25 spacing 20:
                text tooltip.getName(["col", "cap", "noArt"]) xalign 0.5
                text tooltip.description size FSIZE xalign 0.5
                text "Sell: {:,} g / Buy: {:,} g ".format(int(tooltip.price*spread[0]), int(tooltip.price*spread[1])) size FSIZE xalign 0.5


label item_shop(actor, advChar, header, spread = [0.5,3.0], mode = "buy", iFilter = ["all"]):

    #####################################################################
    #                INIT STORE
    #####################################################################

    #advChar "\"How can I help you?\""
    play sound "audio/equip.wav"
    $ yadj = ui.adjustment()

    # shop loop
    while True:

        #####################################################################
        #                PREP INVENTORY
        #####################################################################
        $ actor.inventory.sort(key = lambda x: (itemsort.index(x.kind), x.name))
        $ GAME.mc.inventory.sort(key = lambda x: (itemsort.index(x.kind), x.name))


        $ GAME.mc.inventory.sort(key = lambda x: (x.kind, x.name))
        $ special_wares = [x for x in GAME.ship.cargo if x.ID in ["ITMDrugs"]]
        
        #####################################################################
        #                SHOW SHOP
        #####################################################################
        call screen item_shop_scr(actor.inventory, actor.name.upper(),  GAME.mc.inventory + special_wares, "YOUR ITEMS", spread, mode, yadj)

        #####################################################################
        #                LEAVE
        #####################################################################
        if not _return:
            advChar "\"Until next time...\""
            call quest_updater() from _call_quest_updater_175 
            return()


        #####################################################################
        #                INTERCEPTS BUY
        #####################################################################
        $ cleared = True
        if _return[0] == "buy":
            $ item = _return[1]
            if item.ID == "ITMVoucherPet2" and not GAME.mc.hasAchievement("trained pet owner"):
                play sound "audio/notify.wav"
                advChar "\"Only pets with basic training are eligible for that training.\""
                mc "\"I have not trained my pet yet...\""
                advChar "\"Get your pet the basic training first then...\""
                $ cleared = False
            elif item.ID == "ITMVoucherPet3" and not GAME.mc.hasAchievement("expert pet owner"):
                play sound "audio/notify.wav"
                advChar "\"Only advanced pets are eligible for that training.\""
                mc "\"I don't have an advanced pet yet...\""
                advChar "\"Finish advanced training first then...\""
                $ cleared = False

        #####################################################################
        #                BUY
        #####################################################################
        if _return[1] in GAME.items.values() and _return[0] == "buy" and cleared:
            $ item = _return[1]
            if GAME.money < item.price * spread[1]:
                advChar "\"You cannot afford that item.\""
            else:
                if item.kind in ["pod", "subsys", "subunit", "crate", "ware", "module"]:
                    if GAME.ship.cargoFree() == 0:
                        advChar "\"Your manifest indicates that you cannot load additional items.\""
                        advChar "\"Free up some space first.\""
                    else:
                        $ actor.hasDoneToday("ship_delivery")
                        $ GAME.ship.loadWare(item.ID)
                        $ actor.inventory.remove(item)
                        call transaction("Gear Bought", -int(item.price * spread[1]), True) from _call_transaction_31 

                else:
                    $ GAME.mc.inventory.append(item)
                    $ actor.inventory.remove(item)
                    call transaction("Items Bought", -int(item.price * spread[1]), True) from _call_transaction_32 

        #####################################################################
        #                SELL
        #####################################################################
        elif _return[1] in GAME.items.values() and _return[0] == "sell" and cleared:
            $ item = _return[1]
            if "no_sell" in  item.tags:
                mc "\"I better keep that.\""
                mc "\"I might need it later.\""

            elif item.kind in iFilter or item.ID in iFilter or "all" in iFilter:
                if item in GAME.ship.cargo:
                    $ GAME.ship.unloadWare(item.ID)
                else: 
                    $ GAME.mc.inventory.remove(item)
                $ actor.inventory.append(item)
                call transaction("Wares Sold", int(item.price * spread[0]), True) from _call_transaction_33 

            else:
                advChar "\"I don't deal in these items...\""

    return(_return)

