class World:

    def __init__(self,parent):

        self.parent = parent
        self.width = parent.width
        self.height = parent.height
        self.planet = Planet(parent,self)
        self.objects = []
        self.message = []

        self.satellite = 0

        self.info = [['Temperature',0],['Air pressure',0],['Water',0],['CO2',0],['O2',0],['Satellite',0],['Mine',0],['Nuclear',0],['Algae',0],['Tree',0],['Animal',0]]

    def addObject(self,moverName,location,velocity):
        
        if moverName == '':
            return
        mover = Meteor(self.parent,self,location=location,velocity=velocity)

        if Inventory.items[ShopWindow.items.index(moverName)] == 0:
            return
        Inventory.items[ShopWindow.items.index(moverName)] -= 1

        if moverName == 'Fire':
            mover = Fire(self.parent,self,location=location,velocity=velocity,image='red.png')

        if moverName == 'Ice':
            mover = Ice(self.parent,self,location=location,velocity=velocity,image='blue.png')

        if moverName == 'Dry Ice':
            mover = DryIce(self.parent,self,location=location,velocity=velocity,image='purple.png')

        if moverName == 'Satellite':
            mover = Satellite(self.parent,self,location=location,velocity=velocity,image='satellite.png')
            
        if moverName == 'Mine':
            mover = Mine(self.parent,self,location=location,velocity=velocity,image='mine.png')
            
        if moverName == 'Nuclear':
            mover = Nuclear(self.parent,self,location=location,velocity=velocity,image='nuclear.png')
            
        if moverName == 'Algae':
            mover = Algae(self.parent,self,location=location,velocity=velocity,image='algae.png')

        if moverName == 'Tree':
            mover = Tree(self.parent,self,location=location,velocity=velocity,image='tree.png')
            
        if moverName == 'Animal':
            mover = Animal(self.parent,self,location=location,velocity=velocity,image='animal.png')

        self.objects.append(mover)

    def run(self):

        self.objects = [m for m in self.objects if not m.isDead()]
        self.satellite = 0
        for m in self.objects:
            if type(m) == type(Satellite(self.parent,self)):
                self.satellite += 1
            m.run()
            m.applyForce(Gravity.getForce(m.location,self.planet.location))

            for m2 in self.objects:
                if m == m2:
                    continue
                if m.isCollide() or m2.isCollide():
                    continue
                if m.isCollide(m2):
                    m.explosion()
                    m2.explosion()

        self.planet.run()

        self.update()

        self.output()

    def update(self):
        self.info[0][1] = self.planet.temperature
        self.info[1][1] = self.planet.atmosphere
        self.info[2][1] = self.planet.water
        self.info[3][1] = self.planet.co2
        self.info[4][1] = self.planet.oxygen
        self.info[5][1] = self.satellite
        self.info[6][1] = self.planet.mine
        self.info[7][1] = self.planet.nuclear
        self.info[8][1] = self.planet.algae
        self.info[9][1] = self.planet.tree
        self.info[10][1] = self.planet.animal


    def output(self):

        margin = 0
        length = len(self.message)

        for i in range(length-1,-1,-1):
            margin = self.height-100-(length-i)*50
            text = self.message[i][0]
            cnt  = self.message[i][1]

            if cnt > 112:
                color = QColor(255,255,255,(128-cnt)*16)
            elif cnt < 16:
                color = QColor(255,255,255,cnt*16)
            else:
                color = QColor(255,255,255)

            pen = QColor(color)
            font = QFont('Consolas',15)

            qp = QPainter()
            qp.begin(self.parent)
            qp.setPen(pen)
            qp.setFont(font)
            qp.drawText(50,margin,text)
            qp.end()

            if cnt == 0:
                self.message.pop(i)
            else:
                self.message[i][1] -= 1

    def getInfo(self,id):
        if id >= 8:
            return self.planet.getAmount(self.info[id][1])
        if id >= 5:
            return str(self.info[id][1])
        if id >= 2:
            return self.planet.getAmount(self.info[id][1])
        return self.planet.getStatus(self.info[id][1])