import copy
# import sys, os
# sys.path.append(os.path.abspath(os.path.join('backend','vehicle')))
# print(os.curdir)
from vehicle import vehicle

"""
always the most priority vehicle will charge
all reference will be same
"""

class arrange:
    def __init__(self,capacity=8,queueCapacity=1000):
        self.vehicles=[]
        self.mostPriorVehicle=None
        self.chargedVehicles=[]
        self.noOfCars=0
        self.queue=[]
        self.capacity=capacity
        self.queueCapacity=queueCapacity
        self.entry={}#to maintain and check which car is in the parkingLOT and update when the car leave

    def clearPreviousData(self):
        print("clear previous Data")
        self.vehicles = []
        self.mostPriorVehicle = None
        self.chargedVehicles = []
        self.noOfCars = 0




    def carIsFullyCharged(self,carIndex: list):
        '''

        :param carIndex:  TYPE list all the indexes
        :return:
        '''
        noOfcarShouldTakeFromQueue=len(carIndex)
        tcarList = []
        for i in range(noOfcarShouldTakeFromQueue):
            car=self.vehicles[carIndex[i]-i]
            self.chargedVehicles.append(car)
            #fixme: for timebeing it is like joggar
            #todo: update it after downArrangement
            del self.vehicles[carIndex[i]-i]
            if self.queue!=[]:
                tcarList.append(self.queue[0])
                del self.queue[0]

        for car in self.vehicles:
            tcarList.append(car)
        self.vehicles=[]
        self.noOfCars=0
        for car in tcarList:
            self.addVehicle(car)







    def getMostPriorVehicle(self):
        if self.mostPriorVehicle!=None:
            return self.mostPriorVehicle
        else:
            return None
    def ToChargedVehicles(self,index=0):
        self.noOfCars-=1
        if index==0 and self.noOfCars>0:
            self.vehicles[0]=self.vehicles[self.noOfCars]
            self.vehicles=self.vehicles[:self.noOfCars]
            self.downArrange()


        self.assignMostPriorVehicle()


    def assignMostPriorVehicle(self):
        if self.noOfCars>0:
            self.mostPriorVehicle=self.vehicles[0]
        else:
            self.mostPriorVehicle=None
    def addVehicle(self,vehicle):
        print("new vehicle is arived")
        if self.noOfCars==self.capacity:
            print('charging capacity is fulled')
            if len(self.queue)<self.queueCapacity:
                self.queue.append(vehicle)
                self.entry[vehicle.id]=True
            else:
                print('parking lot is also full')
            return
        self.noOfCars+=1
        if self.mostPriorVehicle==None:
            self.mostPriorVehicle=vehicle
            self.vehicles.append(self.mostPriorVehicle)
        else:
            if vehicle.timeToBeInParkingLot<self.mostPriorVehicle.timeToBeInParkingLot:
                self.vehicles.append(copy.deepcopy(self.mostPriorVehicle))
                self.mostPriorVehicle = vehicle
                self.vehicles[0] = vehicle
            else:
                self.vehicles.append(vehicle)
            self.upArrange(self.noOfCars-1)
        self.entry[vehicle.id] = True

        # for cars in self.vehicles:
        #     print(cars.timeToBeInParkingLot,cars.requiredPower)



    def removeTheMostPriorVehicle(self):
        self.noOfCars -= 1
        del self.entry[self.mostPriorVehicle.id]
        if(self.noOfCars<0):
            self.noOfCars=0

        if(len(self.queue)!=0):
            tempCar=self.queue[0]
            del self.queue[0]
            self.noOfCars+=1
            self.addVehicle(tempCar)

        if(len(self.vehicles)<2):
            self.vehicles=[]
        else:
            self.vehicles[0]=self.vehicles[-1]
            self.vehicles=self.vehicles[0:len(self.vehicles)-1]
            self.downArrange()

        self.assignMostPriorVehicle()
        for cars in self.vehicles:
            print(cars.timeToBeInParkingLot,cars.requiredPower)










    def upArrange(self,index):
        if(index!=0):
            parentIndex=index//2
            childVehicle=self.vehicles[index]
            parentVehicle=self.vehicles[parentIndex]
            if(childVehicle.timeToBeInParkingLot<parentVehicle.timeToBeInParkingLot):
                self.vehicles[parentIndex]=childVehicle
                self.vehicles[index]=parentVehicle
                self.upArrange(parentIndex)
            return
        return


    def downArrange(self):
        if self.noOfCars<1:
            return
        index=0
        while True:
            rightChildIndex=(2*index)+1
            leftChildIndex=(2*index)+2
            if leftChildIndex>=self.noOfCars:
                return
            changeAble=False
            changedToIndex=leftChildIndex
            if rightChildIndex<self.noOfCars and self.vehicles[rightChildIndex].timeToBeInParkingLot<self.vehicles[leftChildIndex].timeToBeInParkingLot:
                changedToIndex=rightChildIndex
            if self.vehicles[changedToIndex].timeToBeInParkingLot<self.vehicles[index].timeToBeInParkingLot:
                changeAble=True


            if changeAble:
                tempVehicle=self.vehicles[changedToIndex]
                self.vehicles[changedToIndex]=self.vehicles[index]
                self.vehicles[index]=tempVehicle
                index=changedToIndex
            else:
                return









































if __name__ == '__main__':
    v1=vehicle(1,1,1,20)
    v2=vehicle(1,1,20,20)
    v3=vehicle(1,1,5,20)
    v4=vehicle(1,1,3,20)
    # v31 = vehicle(1, 1, 0, 20)
    v41 = vehicle(1, 1, 2, 20)

    park=arrange()
    park.addVehicle(v1)
    park.addVehicle(v2)
    park.addVehicle(v3)
    park.addVehicle(v4)
    # park.addVehicle(v31)
    park.addVehicle(v41)

    t=[]
    for x in park.vehicles:
        t.append(x.timeToBeInParkingLot)
    print(t)







