#!/usr/bin/env python

import logging

import base
from kcaa import screens


logger = logging.getLogger('kcaa.manipulators.logistics')


class ChargeFleet(base.Manipulator):

    def run(self, fleet_id):
        fleet_id = int(fleet_id)
        logger.info('Charging fleet {}'.format(fleet_id))
        ship_list = self.objects['ShipList']
        fleet_list = self.objects['FleetList']
        fleet = fleet_list.fleets[fleet_id - 1]
        if fleet.mission_id:
            raise Exception('Fleet {} is away for mission.'.format(fleet_id))
        resource_full = True
        for ship_id in fleet.ship_ids:
            ship = ship_list.ships[str(ship_id)]
            if not ship.resource_full:
                resource_full = False
                break
        if resource_full:
            logger.debug('Fleet {} is full of resources.'.format(fleet_id))
            return
        yield self.screen.change_screen(screens.PORT_LOGISTICS)
        yield self.screen.select_fleet(fleet_id)
        yield self.screen.select_all_members()
        yield self.screen.charge_both()


class ChargeAllFleets(base.Manipulator):

    def run(self):
        ship_list = self.objects['ShipList']
        fleet_list = self.objects['FleetList']
        for fleet in fleet_list.fleets:
            ships = [ship_list.ships[str(ship_id)] for ship_id in
                     fleet.ship_ids]
            if not fleet.mission_id and any([not ship.resource_full for ship in
                                             ships]):
                yield self.do_manipulator(ChargeFleet, fleet_id=fleet.id)


class AutoChargeFleet(base.AutoManipulator):

    @classmethod
    def monitored_objects(cls):
        return ['ShipList', 'FleetList']

    @classmethod
    def can_trigger(cls, owner):
        if not screens.in_category(owner.screen_id, screens.PORT):
            return
        fleet_list = owner.objects.get('FleetList')
        ship_list = owner.objects.get('ShipList')
        fleet_ids_to_charge = []
        for fleet in fleet_list.fleets:
            if fleet.mission_id:
                continue
            for ship_id in fleet.ship_ids:
                ship = ship_list.ships[str(ship_id)]
                loaded = ship.loaded_resource
                capacity = ship.resource_capacity
                if loaded is None or capacity is None:
                    logger.debug(u'Ship {} ({}) looks incomplete.'.format(
                        ship.name, ship.id))
                    logger.debug(str(ship))
                if loaded.fuel < capacity.fuel or loaded.ammo < capacity.ammo:
                    fleet_ids_to_charge.append(fleet.id)
                    break
        if fleet_ids_to_charge:
            return {'fleet_ids': fleet_ids_to_charge}

    def run(self, fleet_ids):
        yield 1.0
        for fleet_id in fleet_ids:
            yield self.do_manipulator(ChargeFleet, fleet_id)

#!/usr/bin/env python

import logging

import base
from kcaa import screens


logger = logging.getLogger('kcaa.manipulators.logistics')


class ChargeFleet(base.Manipulator):

    def run(self, fleet_id):
        fleet_id = int(fleet_id)
        logger.info('Charging fleet {}'.format(fleet_id))
        ship_list = self.objects['ShipList']
        fleet_list = self.objects['FleetList']
        fleet = fleet_list.fleets[fleet_id - 1]
        if fleet.mission_id:
            raise Exception('Fleet {} is away for mission.'.format(fleet_id))
        resource_full = True
        for ship_id in fleet.ship_ids:
            ship = ship_list.ships[str(ship_id)]
            if not ship.resource_full:
                resource_full = False
                break
        if resource_full:
            logger.debug('Fleet {} is full of resources.'.format(fleet_id))
            return
        yield self.screen.change_screen(screens.PORT_LOGISTICS)
        yield self.screen.select_fleet(fleet_id)
        yield self.screen.select_all_members()
        yield self.screen.charge_both()


class ChargeAllFleets(base.Manipulator):

    def run(self):
        ship_list = self.objects['ShipList']
        fleet_list = self.objects['FleetList']
        for fleet in fleet_list.fleets:
            ships = [ship_list.ships[str(ship_id)] for ship_id in
                     fleet.ship_ids]
            if not fleet.mission_id and any([not ship.resource_full for ship in
                                             ships]):
                yield self.do_manipulator(ChargeFleet, fleet_id=fleet.id)


class AutoChargeFleet(base.AutoManipulator):

    @classmethod
    def monitored_objects(cls):
        return ['ShipList', 'FleetList']

    @classmethod
    def can_trigger(cls, owner):
        if not screens.in_category(owner.screen_id, screens.PORT):
            return
        fleet_list = owner.objects.get('FleetList')
        ship_list = owner.objects.get('ShipList')
        fleet_ids_to_charge = []
        for fleet in fleet_list.fleets:
            if fleet.mission_id:
                continue
            for ship_id in fleet.ship_ids:
                ship = ship_list.ships[str(ship_id)]
                loaded = ship.loaded_resource
                capacity = ship.resource_capacity
                if loaded is None or capacity is None:
                    logger.debug(u'Ship {} ({}) looks incomplete.'.format(
                        ship.name, ship.id))
                    logger.debug(str(ship))
                if loaded.fuel < capacity.fuel or loaded.ammo < capacity.ammo:
                    fleet_ids_to_charge.append(fleet.id)
                    break
        if fleet_ids_to_charge:
            return {'fleet_ids': fleet_ids_to_charge}

    def run(self, fleet_ids):
        yield 1.0
        for fleet_id in fleet_ids:
            yield self.do_manipulator(ChargeFleet, fleet_id)

#!/usr/bin/env python