import attr
from aioice import Candidate as RTCIceCandidate  # noqa
from aioice import Connection
from pyee import EventEmitter


class RTCIceGatherer(EventEmitter):
    """
    The :class:`RTCIceGatherer` interface gathers local host, server reflexive
    and relay candidates, as well as enabling the retrieval of local
    Interactive Connectivity Establishment (ICE) parameters which can be
    exchanged in signaling.
    """
    def __init__(self):
        super().__init__()
        self._connection = Connection(ice_controlling=False,
                                      stun_server=('stun.l.google.com', 19302))
        self.__state = 'new'

    @property
    def state(self):
        """
        The current state of the ICE gatherer.
        """
        return self.__state

    async def gather(self):
        """
        Gather ICE candidates.
        """
        self.__setState('gathering')
        await self._connection.gather_candidates()
        self.__setState('complete')

    def getLocalCandidates(self):
        """
        Retrieve the list of valid local candidates associated with the ICE
        gatherer.
        """
        return self._connection.local_candidates

    def getLocalParameters(self):
        """
        Retrieve the ICE parameters of the ICE gatherer.

        :rtype: RTCIceParameters
        """
        return RTCIceParameters(
            usernameFragment=self._connection.local_username,
            password=self._connection.local_password)

    def __setState(self, state):
        self.__state = state
        self.emit('statechange')


@attr.s
class RTCIceParameters:
    """
    The :class:`RTCIceParameters` dictionary includes the ICE username
    fragment and password and other ICE-related parameters.
    """
    usernameFragment = attr.ib(default=None)
    "ICE username fragment."

    password = attr.ib(default=None)
    "ICE password."


class RTCIceTransport:
    """
    The :class:`RTCIceTransport` interface allows an application access to
    information about the Interactive Connectivity Establishment (ICE)
    transport over which packets are sent and received.
    """
    def __init__(self, gatherer):
        self._iceGatherer = gatherer

    @property
    def iceGatherer(self):
        """
        The ICE gatherer passed in the constructor.
        """
        return self._iceGatherer

    @property
    def role(self):
        """
        The current role of the ICE transport: `"controlling"` or `"controlled"`.
        """
        if self._connection.ice_controlling:
            return 'controlling'
        else:
            return 'controlled'

    def getRemoteCandidates(self):
        """
        Retrieve the list of candidates associated with the remote
        :class:`RTCIceTransport`.
        """
        return self._connection.remote_candidates

    def setRemoteCandidates(self, remoteCandidates):
        """
        Set the list of candidates associated with the remote
        :class:`RTCIceTransport`.
        """
        self._connection.remote_candidates = remoteCandidates

    async def start(self, remoteParameters):
        """
        Initiate connectivity checks.
        """
        self._connection.remote_username = remoteParameters.usernameFragment
        self._connection.remote_password = remoteParameters.password
        await self._connection.connect()

    async def stop(self):
        """
        Irreversibly stop the :class:`RTCIceTransport`.
        """
        await self._connection.close()

    @property
    def _connection(self):
        return self._iceGatherer._connection

@@ -985,16 +985,17 @@
 complete
+d
 ')%0A%0A    
@@ -1922,16 +1922,30 @@
 ransport
+(EventEmitter)
 :%0A    %22%22
@@ -2186,24 +2186,51 @@
  gatherer):%0A
+        super().__init__()%0A
         self
@@ -2227,24 +2227,25 @@
       self._
+_
 iceGatherer 
@@ -2250,24 +2250,53 @@
 r = gatherer
+%0A        self.__state = 'new'
 %0A%0A    @prope
@@ -2415,32 +2415,33 @@
    return self._
+_
 iceGatherer%0A%0A   
@@ -2702,16 +2702,152 @@
 olled'%0A%0A
+    @property%0A    def state(self):%0A        %22%22%22%0A        The current state of the ICE transport.%0A        %22%22%22%0A        return self.__state%0A%0A
     def 
@@ -3384,32 +3384,68 @@
 ks.%0A        %22%22%22%0A
+        self.__setState('checking')%0A
         self._co
@@ -3614,16 +3614,53 @@
 onnect()
+%0A        self.__setState('completed')
 %0A%0A    as
@@ -3751,32 +3751,109 @@
 t%60.%0A        %22%22%22%0A
+        if self.state != 'closed':%0A            self.__setState('closed')%0A    
         await se
@@ -3937,17 +3937,16 @@
 rn self.
-_
 iceGathe
@@ -3953,16 +3953,112 @@
 rer._connection%0A
+%0A    def __setState(self, state):%0A        self.__state = state%0A        self.emit('statechange')%0A

make RTCIceTransport emit 'statechange' event