# DO NOT EDIT THIS FILE!
#
# This file is generated from the CDP specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# CDP domain: Ads (experimental)
from __future__ import annotations
from .util import event_class, T_JSON_DICT
from dataclasses import dataclass
import enum
import typing
from . import page


@dataclass
class AdFrameData:
    '''
    Ad frame data.
    '''
    #: The DevTools frame token.
    frame_id: page.FrameId

    #: The network bytes of the frame.
    network_bytes: float

    #: The CPU time of the frame, in milliseconds.
    cpu_time: float

    #: The initial origin of the frame. To minimize the payload size, this is
    #: only sent once per frame.
    initial_origin: typing.Optional[str] = None

    def to_json(self):
        json = dict()
        json['frameId'] = self.frame_id.to_json()
        json['networkBytes'] = self.network_bytes
        json['cpuTime'] = self.cpu_time
        if self.initial_origin is not None:
            json['initialOrigin'] = self.initial_origin
        return json

    @classmethod
    def from_json(cls, json):
        return cls(
            frame_id=page.FrameId.from_json(json['frameId']),
            network_bytes=float(json['networkBytes']),
            cpu_time=float(json['cpuTime']),
            initial_origin=str(json['initialOrigin']) if 'initialOrigin' in json else None,
        )


@dataclass
class AdMetrics:
    '''
    Ad metrics for a page.
    '''
    #: The viewport ad density by area, represented as a percentage (an integer
    #: between 0 and 100).
    viewport_ad_density_by_area: int

    #: The time-weighted average of the viewport ad density by area, measured
    #: across the duration of the page.
    average_viewport_ad_density_by_area: float

    #: The number of ads currently visible within the viewport.
    viewport_ad_count: int

    #: The time-weighted average of the viewport ad count, measured across the
    #: duration of the page.
    average_viewport_ad_count: float

    #: The total ad CPU usage, in milliseconds.
    total_ad_cpu_time: float

    #: The total ad network bytes.
    total_ad_network_bytes: float

    #: The list of ad frames that have been updated since the last event.
    update_ad_frames: typing.List[AdFrameData]

    #: The list of ad frame IDs that have been removed since the last event.
    remove_ad_frames: typing.List[page.FrameId]

    def to_json(self):
        json = dict()
        json['viewportAdDensityByArea'] = self.viewport_ad_density_by_area
        json['averageViewportAdDensityByArea'] = self.average_viewport_ad_density_by_area
        json['viewportAdCount'] = self.viewport_ad_count
        json['averageViewportAdCount'] = self.average_viewport_ad_count
        json['totalAdCpuTime'] = self.total_ad_cpu_time
        json['totalAdNetworkBytes'] = self.total_ad_network_bytes
        json['updateAdFrames'] = [i.to_json() for i in self.update_ad_frames]
        json['removeAdFrames'] = [i.to_json() for i in self.remove_ad_frames]
        return json

    @classmethod
    def from_json(cls, json):
        return cls(
            viewport_ad_density_by_area=int(json['viewportAdDensityByArea']),
            average_viewport_ad_density_by_area=float(json['averageViewportAdDensityByArea']),
            viewport_ad_count=int(json['viewportAdCount']),
            average_viewport_ad_count=float(json['averageViewportAdCount']),
            total_ad_cpu_time=float(json['totalAdCpuTime']),
            total_ad_network_bytes=float(json['totalAdNetworkBytes']),
            update_ad_frames=[AdFrameData.from_json(i) for i in json['updateAdFrames']],
            remove_ad_frames=[page.FrameId.from_json(i) for i in json['removeAdFrames']],
        )


def get_ad_metrics() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,AdMetrics]:
    '''
    Retrieves ad metrics for the current page.

    :returns: 
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Ads.getAdMetrics',
    }
    json = yield cmd_dict
    return AdMetrics.from_json(json['metrics'])
