#
# Copyright 2023, Ali Arya
# Anyone Can Code: Algorithmic Thinking
# Example 5.13p
#

import random

#Record types
class Player:
    """A simple Player class/record"""
    ID = 0
    Name = " "
    Score = 0

class PlayerListItem:
    """A class/record for player list items"""
    player = None
    next = None
    previous = None

#Global variables
firstPlayerItem = None
lastPlayerItem = None
newPlayerItem = None
numPlayers = 0

#list functions
def CreatePlayer():
    global firstPlayerItem
    global lastPlayerItem
    global newPlayerItem
    global numPlayers
    p = Player()
    p.ID = 1000 + random.randint(0,100)
    newPlayerItem = PlayerListItem()
    newPlayerItem.player = p
    newPlayerItem.next = None
    newPlayerItem.previous = None

def AddPlayer():
    global firstPlayerItem
    global lastPlayerItem
    global newPlayerItem
    global numPlayers

    if newPlayerItem == None:
        return
    if firstPlayerItem == None:
        firstPlayerItem = newPlayerItem
        lastPlayerItem = newPlayerItem
    else:
        lastPlayerItem.next = newPlayerItem
        newPlayerItem.previous = lastPlayerItem
        lastPlayerItem = newPlayerItem
    numPlayers += 1
    newPlayerItem = None

def InsertPlayer(i):
    global firstPlayerItem
    global lastPlayerItem
    global newPlayerItem
    global numPlayers

    if newPlayerItem == None:
        return
    if lastPlayerItem == None:
        firstPlayerItem = newPlayerItem
        lastPlayerItem = newPlayerItem
    elif i >= numPlayers:
        lastPlayerItem.next = newPlayerItem
        newPlayerItem.previous = lastPlayerItem
        lastPlayerItem = newPlayerItem
    else:
        n = 0
        tempPlayerItem = firstPlayerItem
        prevPlayerItem = None
        while tempPlayerItem != None:
            if n == i-1:
                prevPlayerItem = tempPlayerItem
            if n == i:
                newPlayerItem.next = tempPlayerItem
                tempPlayerItem.previous = newPlayerItem
                if prevPlayerItem != None:
                    prevPlayerItem.next = newPlayerItem
                if i == 0:
                    firstPlayerItem = newPlayerItem
                break
            else:
                n += 1
                tempPlayerItem = tempPlayerItem.next
        numPlayers += 1
        newPlayerItem = None

def DeletePlayer(i):
    global firstPlayerItem
    global lastPlayerItem
    global newPlayerItem
    global numPlayers
    n = 0
    tempPlayerItem = firstPlayerItem
    prevPlayerItem = None
    while tempPlayerItem != None:
        if n == i-1:
            prevPlayerItem = tempPlayerItem
        if n == i:
            if prevPlayerItem != None:
                prevPlayerItem.next = tempPlayerItem.next
            if tempPlayerItem.next == None:
                lastPlayerItem = prevPlayerItem
            if i == 0:
                firstPlayerItem = tempPlayerItem.next
            #no explicit delete needed
            numPlayers -= 1
            break
        else:
            n += 1
            tempPlayerItem = tempPlayerItem.next



def ReportList():
    global firstPlayerItem
    global lastPlayerItem
    global newPlayerItem
    global numPlayers
    tempPlayerItem = firstPlayerItem
    while tempPlayerItem != None:
        print(tempPlayerItem.player.ID)
        tempPlayerItem = tempPlayerItem.next

#main code
#sample operations
CreatePlayer()
AddPlayer()
CreatePlayer()
AddPlayer()
CreatePlayer()
AddPlayer()
ReportList()

input("insert at 0")
CreatePlayer()
InsertPlayer(0)
ReportList()

input("insert at 1")
CreatePlayer()
InsertPlayer(1)
ReportList()

input("insert at 10")
CreatePlayer()
InsertPlayer(10)
ReportList()

input("delete 0")
CreatePlayer()
DeletePlayer(0)
ReportList()

input("delete 1")
CreatePlayer()
DeletePlayer(1)
ReportList()
