import fnmatch

METADATA_OPTIONS = METADATA_OPTIONS = {
    "Everything (All Metadata)": None,
    "File System Information": [
        "FileModifyDate",
        "FileAccessDate",
        "RelatedSoundFile",
        "OriginalFileName",
        "ThumbnailFileName",
        "RawFileName",
    ],
    "Core Image Properties": [
        "ExifImageWidth",
        "ExifImageHeight",
        "Orientation",
        "SamplesPerPixel",
        "PhotometricInterpretation",
        "ExifByteOrder",
        "JFIFVersion",
        "ComponentsConfiguration",
        "ColorComponents",
    ],
    "Camera and Exposure Settings (EXIF)": [
        "Make",
        "Model",
        "Software",
        "ModifyDate",
        "DateTimeOriginal",
        "CreateDate",
        "ExposureTime",
        "FNumber",
        "ISO",
        "ShutterSpeedValue",
        "ApertureValue",
        "ExposureCompensation",
        "ExposureProgram",
        "MeteringMode",
        "Flash",
        "LightSource",
        "SubSecTime",
        "SubSecTimeOriginal",
        "SubSecTimeDigitized",
        "SubSecCreateDate",
        "SubSecDateTimeOriginal",
        "SubSecModifyDate",
        "FlashpixVersion",
        "ColorSpace",
        "CustomRendered",
        "ExposureMode",
        "WhiteBalance",
        "SceneCaptureType",
        "DigitalZoomRatio",
        "GainControl",
        "Contrast",
        "Saturation",
        "Sharpness",
        "SubjectDistanceRange",
        "SensingMethod",
        "FileSource",
        "SceneType",
        "MaxApertureValue",
        "BrightnessValue",
        "HDRImageType",
        "SensitivityType",
        "ISOSpeed",
    ],
    "Lens and Focus Details": [
        "FocalLength",
        "FocalLengthIn35mmFormat",
        "FocalPlaneXResolution",
        "FocalPlaneYResolution",
        "FocalPlaneResolutionUnit",
        "SubjectArea",
        "SubjectDistance",
        "LensInfo",
        "LensMake",
        "LensModel",
        "LensID",
        "Lens",
        "Lens35efl",
        "FocusMode",
        "AFPoint",
        "FocusRange",
    ],
    "Calculated Photographic Properties": [
        "ScaleFactor35efl",
        "ShutterSpeed",
        "Aperture",
        "CircleOfConfusion",
        "DOF",
        "FOV",
        "FocalLength35efl",
        "HyperfocalDistance",
        "LightValue",
    ],
    "GPS Geolocation Data": ["GPS*"],
    "Content and Authorship (IPTC & XMP)": [
        "ImageDescription",
        "Artist",
        "Copyright",
        "UserComment",
        "CreatorTool",
        "OwnerName",
        "Headline",
        "Instructions",
        "AuthorsPosition",
        "Credit",
        "Source",
        "City",
        "State",
        "Country",
        "TransmissionReference",
        "DateCreated",
        "TimeCreated",
        "CaptionWriter",
        "Title",
        "Rights",
        "Comment",
        "XPAuthor",
        "XPComment",
        "XPKeywords",
        "XPSubject",
        "XPTitle",
        "Rating",
        "CopyrightNotice",
        "By-line",
        "ObjectName",
        "Keywords",
        "Caption-Abstract",
        "Description",
        "Creator",
        "Subject",
        "UsageTerms",
        "CreatorWorkEmail",
        "HierarchicalSubject",
        "Label",
        "WebStatement",
    ],
    "Adobe XMP Metadata": [
        "XMPToolkit",
        "Format",
        "ColorMode",
        "ICCProfileName",
        "TextLayerName",
        "TextLayerText",
        "DocumentAncestors",
        "InstanceID",
        "DerivedFromInstanceID",
        "DerivedFromDocumentID",
        "DocumentID",
        "OriginalDocumentID",
        "HistoryAction",
        "HistoryInstanceID",
        "HistoryWhen",
        "HistorySoftwareAgent",
        "HistoryChanged",
        "HistoryParameters",
        "Marked",
        "VersionID",
        "Logging",
        "Channel",
        "SubFolder",
        "LastKeywordXMP",
        "LastKeywordIPTC",
        "NativeDigest",
        "About",
        "Creatortool",
        "OffsetSchema",
    ],
    "Embedded Thumbnail & Preview": [
        "ThumbnailImage",
        "ThumbnailOffset",
        "ThumbnailLength",
        "PreviewImage",
        "PreviewImageValid",
        "PreviewImageStart",
        "PreviewImageLength",
    ],
    "ICC Color Profile": [
        "ProfileCMMType",
        "ProfileVersion",
        "ProfileClass",
        "ColorSpaceData",
        "ProfileConnectionSpace",
        "ProfileDateTime",
        "ProfileFileSignature",
        "PrimaryPlatform",
        "CMMFlags",
        "DeviceManufacturer",
        "DeviceModel",
        "DeviceAttributes",
        "RenderingIntent",
        "ConnectionSpaceIlluminant",
        "ProfileCreator",
        "ProfileID",
        "ProfileCopyright",
        "ProfileDescription",
        "MediaWhitePoint",
        "MediaBlackPoint",
        "RedMatrixColumn",
        "GreenMatrixColumn",
        "BlueMatrixColumn",
        "DeviceMfgDesc",
        "DeviceModelDesc",
        "ViewingCondDesc",
        "ViewingCondIlluminant",
        "ViewingCondSurround",
        "ViewingCondIlluminantType",
        "Luminance",
        "MeasurementObserver",
        "MeasurementBacking",
        "MeasurementGeometry",
        "MeasurementFlare",
        "MeasurementIlluminant",
        "Technology",
        "RedTRC",
        "GreenTRC",
        "BlueTRC",
        "ProfileDescriptionML*",
        "ChromaticAdaptation",
    ],
    "Technical Image Composition": [
        "Compression",
        "EncodingProcess",
        "BitsPerSample",
        "YCbCrSubSampling",
        "YCbCrPositioning",
        "XResolution",
        "YResolution",
        "ResolutionUnit",
        "CompressedBitsPerPixel",
        "PlanarConfiguration",
        "StripOffsets",
        "RowsPerStrip",
        "StripByteCounts",
        "JPEGInterchangeFormat*",
        "Padding",
    ],
    "Multi-Picture Format (MPF)": [
        "MPFVersion",
        "NumberOfImages",
        "MPImageFlags",
        "MPImageFormat",
        "MPImageType",
        "MPImageLength",
        "MPImageStart",
        "DependentImage1EntryNumber",
        "DependentImage2EntryNumber",
    ],
    "Canon MakerNotes": [
        "MacroMode",
        "SelfTimer",
        "Quality",
        "CanonFlashMode",
        "ContinuousDrive",
        "RecordMode",
        "CanonImageSize",
        "EasyMode",
        "DigitalZoom",
        "Contrast",
        "Saturation",
        "Sharpness",
        "CameraISO",
        "FocusRange",
        "CanonExposureMode",
        "LensType",
        "MaxFocalLength",
        "MinFocalLength",
        "FocalUnits",
        "MaxAperture",
        "MinAperture",
        "FlashActivity",
        "FlashBits",
        "FocusContinuous",
        "AESetting",
        "ImageStabilization",
        "ZoomSourceWidth",
        "ZoomTargetWidth",
        "SpotMeteringMode",
        "ManualFlashOutput",
        "AutoISO",
        "BaseISO",
        "MeasuredEV",
        "TargetAperture",
        "TargetExposureTime",
        "SlowShutter",
        "SequenceNumber",
        "OpticalZoomCode",
        "FlashGuideNumber",
        "FlashExposureComp",
        "AutoExposureBracketing",
        "AEBBracketValue",
        "ControlMode",
        "FocusDistanceUpper",
        "FocusDistanceLower",
        "BulbDuration",
        "CameraType",
        "AutoRotate",
        "NDFilter",
        "SelfTimer2",
        "FlashOutput",
        "CanonImageType",
        "CanonFirmwareVersion",
        "FileNumber",
        "CanonModelID",
        "DateStampMode",
        "MyColorMode",
        "FirmwareRevision",
        "Categories",
        "IntelligentContrast",
        "ImageUniqueID",
        "FacesDetected",
        "TimeZone",
        "TimeZoneCity",
        "DaylightSavings",
        "AFAreaMode",
        "NumAFPoints",
        "ValidAFPoints",
        "CanonImageWidth",
        "CanonImageHeight",
        "AFImageWidth",
        "AFImageHeight",
        "AFAreaWidths",
        "AFAreaHeights",
        "AFAreaXPositions",
        "AFAreaYPositions",
        "AFPointsInFocus",
        "PrimaryAFPoint",
        "AspectRatio",
        "CroppedImageWidth",
        "CroppedImageHeight",
        "CroppedImageLeft",
        "CroppedImageTop",
        "VRDOffset",
    ],
    "Nikon MakerNotes": [
        "MakerNoteVersion",
        "ColorMode",
        "Quality",
        "WhiteBalance",
        "FlashSetting",
        "FlashType",
        "ISOSelection",
        "ImageAdjustment",
        "ToneComp",
        "AuxiliaryLens",
        "AFAreaMode",
        "AFPoint",
        "AFPointsInFocus",
        "SceneMode",
        "DistortionControl",
        "SaturationAdj",
        "NoiseReduction",
        "RetouchHistory",
        "ImageStabilization",
        "PictureControl*",
        "ActiveD-Lighting",
    ],
    "Olympus MakerNotes": [
        "SpecialMode",
        "CameraID",
        "EquipmentVersion",
        "CameraType2",
        "SerialNumber",
        "InternalSerialNumber",
        "FocalPlaneDiagonal",
        "BodyFirmwareVersion",
        "AELock",
        "MacroMode",
        "FocusProcess",
        "AFSearch",
        "AFAreas",
        "FlashMode",
        "WhiteBalance2",
        "WhiteBalanceTemperature",
        "CustomSaturation",
        "ModifiedSaturation",
        "ContrastSetting",
        "SharpnessSetting",
        "NoiseReduction",
        "DistortionCorrection",
        "ShadingCompensation",
        "Gradation",
        "NoiseFilter",
        "DriveMode",
        "PanoramaMode",
        "ImageStabilization",
        "FacesDetected",
        "FaceDetectArea",
    ],
    "Fujifilm MakerNotes": [
        "Version",
        "Quality",
        "Sharpness",
        "WhiteBalance",
        "Saturation",
        "FujiFlashMode",
        "FlashExposureComp",
        "Macro",
        "FocusMode",
        "SlowSync",
        "PictureMode",
        "AutoBracketing",
        "BlurWarning",
        "FocusWarning",
        "ExposureWarning",
    ],
    "Casio MakerNotes": [
        "PreviewImageSize",
        "FirmwareDate",
        "WhiteBalanceBias",
        "ObjectDistance",
        "FlashDistance",
        "RecordMode",
        "ReleaseMode",
        "Quality",
        "FocusMode",
        "HometownCity",
        "BestShotMode",
        "Enhancement",
        "ColorFilter",
        "ArtMode",
        "BracketSequence",
    ],
    "Other Manufacturer Data": [
        "SanyoQuality",
        "Macro",
        "DigitalZoom",
        "DataDump",
        "BWMode",
        "LensDistortionParams",
        "CameraType",
        "PrintIMVersion",
        "FirmwareVersion",
        "RedGain",
        "BlueGain",
        "GreenGain",
        "OffsetTime*",
        "RunTime*",
        "AEStable",
        "AETarget",
        "AEAverage",
        "AFStable",
        "AccelerationVector",
        "TriggerMode",
        "Sequence",
        "EventNumber",
        "MoonPhase",
        "AmbientTemperature*",
        "InfraredIlluminator",
        "MotionSensitivity",
        "BatteryVoltage",
        "UserLabel",
        "Warning*",
        "MakerNote*",
    ],
}


def get_fields(option_name, available_tags=None):
    patterns = METADATA_OPTIONS.get(option_name)

    if patterns is None:
        return None

    if available_tags is None:
        return list(patterns)

    matched = set()
    for pat in patterns:
        if any(ch in pat for ch in "*?[]"):
            for t in available_tags:
                if fnmatch.fnmatchcase(t, pat):
                    matched.add(t)
        else:
            if pat in available_tags:
                matched.add(pat)

    return list(matched)


def list_options():
    return list(METADATA_OPTIONS.keys())
