#!/usr/local/bin/python3.13

# Copyright 2014 Facundo Batista
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# For further info, check  http://github.com/facundobatista/yaswfp

"""Parse a SWF file and expose all its internals."""

import argparse
import os
import sys

project_basedir = os.path.abspath(os.path.dirname(os.path.dirname(
                                  os.path.realpath(sys.argv[0]))))
if project_basedir not in sys.path:
    sys.path.insert(0, project_basedir)

from yaswfp import swfparser


parser = argparse.ArgumentParser(
    description='Parse a SWF file and show all its internals')
parser.add_argument('filepath', help='the SWF file to parse')
parser.add_argument('-t', '--show-tags', action='store_true',
                    help='show the first level tags of the file')
parser.add_argument('-e', '--extended', action='store_true',
                    help='show all objects with full detail and nested')
parser.add_argument('-c', '--coverage', action='store_true',
                    help='indicate a percentage of coverage of given file')
args = parser.parse_args()

swf = swfparser.parsefile(args.filepath)
print(swf.header)
print("Tags count:", len(swf.tags))

if args.coverage:
    swf.coverage()

if args.show_tags:
    for tag in swf.tags:
        print(tag)

if args.extended:
    def _show(obj, level, prefix=""):
        indent = level * "    "
        indent_sub = indent + "    "

        if isinstance(obj, list):
            prefix_next = "- "
            children = [(item.name, item) for item in obj]
        else:
            prefix_next = ""
            children = [(name, getattr(obj, name)) for name in obj._attribs]
            class_name = obj.__class__.__name__
            if obj.name == class_name:
                tit = obj.name
            else:
                tit = "{} ({})".format(obj.name, class_name)
            if children:
                final = ":"
            else:
                final = ""
            print("{}{}{}{}".format(indent, prefix, tit, final))

        for name, child in children:
            if hasattr(child, '_attribs'):
                # complex child
                _show(child, level + 1, prefix_next)
            elif isinstance(child, list) and child and any(
                    isinstance(x, swfparser.SWFObject) for x in child):
                # special case for a list
                print("{}{}{}:".format(indent_sub, prefix, name))
                _show(child, level + 1, prefix_next)
            else:
                print("{}{}: {!r}".format(indent_sub, name, child))

    for tag in swf.tags:
        _show(tag, 0)
