# Copyright 2021 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

from scout.dao.space import get_spots_by_filter, _get_spot_filters, \
    _get_extended_info_by_key
import copy


def get_item_by_id(item_id):
    spot = get_spots_by_filter([
            ('item:id', item_id),
            ('extended_info:app_type', 'tech')
    ])
    if spot:
        spot = _filter_spot_items(item_id, spot[0])
    return spot


def _filter_spot_items(item_id, spot):
    for item in spot.items:
        if item.item_id == item_id:
            spot.item = item
    return spot


def add_item_info(spot):
    for item in spot.items:
        item.model = _get_extended_info_by_key("i_model",
                                               item.extended_info)
        item.brand = _get_extended_info_by_key("i_brand",
                                               item.extended_info)
        item.checkout_period = _get_extended_info_by_key(
            "i_checkout_period",
            item.extended_info
        )
        item.reservation_notes = _get_extended_info_by_key(
            "i_reservation_notes",
            item.extended_info
        )
        item.is_active = _get_extended_info_by_key(
            "i_is_active",
            item.extended_info
        )
        item.quantity = _get_extended_info_by_key(
            "i_quantity",
            item.extended_info
        )
        item.description = _get_extended_info_by_key(
            "i_description",
            item.extended_info
        )
        item.reserve_url = _get_extended_info_by_key(
            "i_reserve_url",
            item.extended_info
        )
        item.manual_url = _get_extended_info_by_key(
            "i_manual_url",
            item.extended_info
        )
        item.owner = _get_extended_info_by_key(
            "i_owner",
            item.extended_info
        )
        item.is_stf = _get_extended_info_by_key(
            "i_is_stf",
            item.extended_info
        )
        item.cte_type_id = _get_extended_info_by_key(
            "cte_type_id",
            item.extended_info
        )

    return spot


def get_filtered_items(spots, request):
    parameter_list = _get_spot_filters(request)
    brand = []
    subcategory = []
    is_active = False
    for param in parameter_list:
        if param[0] == "item:extended_info:i_brand":
            brand.append(param[1])
        elif param[0] == "item:subcategory":
            subcategory.append(param[1])
        elif param[0] == "item:extended_info:i_is_active":
            is_active = True

    new_spots = []

    for spot in spots:
        new_spot = copy.deepcopy(spot)
        new_spot.items = []
        for item in spot.items:
            if is_active and not item.is_active:
                continue
            if len(subcategory) > 0 and item.subcategory not in subcategory:
                continue
            if len(brand) > 0 and item.brand not in brand:
                continue
            new_spot.items.append(item)
        new_spots.append(new_spot)
    return new_spots


def get_item_count(spots):
    item_count = 0
    for spot in spots:
        item_count += len(spot.items)
    return item_count

[((10, 11, 13, 6), 'scout.dao.space.get_spots_by_filter', 'get_spots_by_filter', ({(10, 31, 13, 5): "[('item:id', item_id), ('extended_info:app_type', 'tech')]"}, {}), "([('item:id', item_id), ('extended_info:app_type', 'tech')])", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((77, 21, 77, 47), 'scout.dao.space._get_spot_filters', '_get_spot_filters', ({(77, 39, 77, 46): 'request'}, {}), '(request)', False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((28, 21, 29, 66), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(28, 47, 28, 56): '"""i_model"""', (29, 47, 29, 65): 'item.extended_info'}, {}), "('i_model', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((30, 21, 31, 66), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(30, 47, 30, 56): '"""i_brand"""', (31, 47, 31, 65): 'item.extended_info'}, {}), "('i_brand', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((32, 31, 35, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(33, 12, 33, 31): '"""i_checkout_period"""', (34, 12, 34, 30): 'item.extended_info'}, {}), "('i_checkout_period', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((36, 33, 39, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(37, 12, 37, 33): '"""i_reservation_notes"""', (38, 12, 38, 30): 'item.extended_info'}, {}), "('i_reservation_notes', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((40, 25, 43, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(41, 12, 41, 25): '"""i_is_active"""', (42, 12, 42, 30): 'item.extended_info'}, {}), "('i_is_active', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((44, 24, 47, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(45, 12, 45, 24): '"""i_quantity"""', (46, 12, 46, 30): 'item.extended_info'}, {}), "('i_quantity', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((48, 27, 51, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(49, 12, 49, 27): '"""i_description"""', (50, 12, 50, 30): 'item.extended_info'}, {}), "('i_description', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((52, 27, 55, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(53, 12, 53, 27): '"""i_reserve_url"""', (54, 12, 54, 30): 'item.extended_info'}, {}), "('i_reserve_url', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((56, 26, 59, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(57, 12, 57, 26): '"""i_manual_url"""', (58, 12, 58, 30): 'item.extended_info'}, {}), "('i_manual_url', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((60, 21, 63, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(61, 12, 61, 21): '"""i_owner"""', (62, 12, 62, 30): 'item.extended_info'}, {}), "('i_owner', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((64, 22, 67, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(65, 12, 65, 22): '"""i_is_stf"""', (66, 12, 66, 30): 'item.extended_info'}, {}), "('i_is_stf', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((68, 27, 71, 9), 'scout.dao.space._get_extended_info_by_key', '_get_extended_info_by_key', ({(69, 12, 69, 25): '"""cte_type_id"""', (70, 12, 70, 30): 'item.extended_info'}, {}), "('cte_type_id', item.extended_info)", False, 'from scout.dao.space import get_spots_by_filter, _get_spot_filters, _get_extended_info_by_key\n'), ((92, 19, 92, 38), 'copy.deepcopy', 'copy.deepcopy', ({(92, 33, 92, 37): 'spot'}, {}), '(spot)', False, 'import copy\n')]