import six


def process_item(dryrun, verbose, item, coll_dict):
    """
    Process one movie or show item.

    Parameters:

      item (plexapi.video.Movie or plexapi.video.Show): movie or show item to
        be processed.

      coll_dict (dict): Collections dictionary from the collections file, with:
        * key (string): ID of the item
        * value (dict): Attributes of the item, as follows:
          - 'section': Title of the section of the item
          - 'title': Title of the item
          - 'year': Year of the item
          - 'collections': List of collection names of the item
    """

    dryrun_str = "Dryrun: " if dryrun else ""

    # If the item is not fully loaded, it may show only a subset of collections.
    if not item.isFullObject():
        item.reload()

    item_collections = []  # List of collection names in item
    if item.collections:
        for c in item.collections:  # list of plexapi.media.Collection
            t = c.tag
            if isinstance(t, six.binary_type):
                t = t.decode('utf-8')
            item_collections.append(t)

    item_id = item.key.split('/')[-1]
    item_section = item.section().title
    item_title = item.title
    item_year = item.year

    if item_id not in coll_dict:
        if verbose:
            print("{d}Creating {s!r} item in collections file: {t!r} ({y})".
                  format(d=dryrun_str, s=item_section, t=item_title,
                         y=item_year))
        coll_dict[item_id] = {
            'section': item_section,
            'title': item_title,
            'year': item_year,
            'collections': [],
        }
        file_item_dict = coll_dict[item_id]
    else:
        file_item_dict = coll_dict[item_id]
        if item_section != file_item_dict.get('section', None) or \
                item_title != file_item_dict.get('title', None) or \
                item_year != file_item_dict.get('year', None):
            if verbose:
                print("{d}Updating section/title/year in collections file for "
                      "{s!r} item: {t!r} ({y})".
                      format(d=dryrun_str, s=item_section, t=item_title,
                             y=item_year))
            file_item_dict['section'] = item_section
            file_item_dict['title'] = item_title
            file_item_dict['year'] = item_year

    # Sync collections from PMS to collections file
    for coll in item_collections:
        if coll not in file_item_dict['collections']:
            if verbose:
                print("{d}Saving collection {c!r} to collections file for "
                      "{s!r} item: {t!r} ({y})".
                      format(d=dryrun_str, c=coll, s=item_section,
                             t=item_title, y=item_year))
            file_item_dict['collections'].append(coll)

    # Sync collections from collections file to PMS
    for coll in file_item_dict['collections']:
        if coll not in item_collections:
            if verbose:
                print("{d}Restoring collection '{c}' from collections file for "
                      "{s!r} item: {t!r} ({y})".
                      format(d=dryrun_str, c=coll, s=item_section,
                             t=item_title, y=item_year))
            item_collections.append(coll)
            if not dryrun:
                item.addCollection(coll)

    return 0