from noise.dh.dh import DH
from noise.cipher.cipher import Cipher
from noise.hash.hash import Hash
from noise.processing.handshakepatterns.handshakepattern import HandshakePattern

from noise.processing.impl.handshakestate import HandshakeState
from noise.processing.impl.symmetricstate import SymmetricState
from noise.processing.impl.cipherstate import CipherState


class NoiseProtocol(object):
    def __init__(self, pattern, dh, cipher, hash):
        """
        :param pattern:
        :type pattern:
        :param dh:
        :type dh:
        :param cipher:
        :type cipher:
        :param hash:
        :type hash:
        """
        self._pattern = pattern  # type: HandshakePattern
        self._dh = dh  # type: DH
        self._cipher = cipher  # type: Cipher
        self._hash = hash  # type: Hash
        self._oneway = len(HandshakePattern.parse_handshakepattern(pattern.name)[0]) == 1  # type: bool

    @property
    def oneway(self):
        return self._oneway

    @property
    def pattern(self):
        return self._pattern

    @property
    def dh(self):
        return self._dh

    @property
    def cipher(self):
        return self._cipher

    @property
    def hash(self):
        return self._hash

    def create_cipherstate(self, cipher=None):
        """
        :param cipher:
        :type cipher: Cipher
        :return:
        :rtype: CipherState
        """
        return CipherState(cipher or self._cipher)

    def create_symmetricstate(self, cipherstate=None, hash=None):
        """
        :param cipherstate:
        :type cipherstate: CipherState
        :param hash:
        :type hash: Hash
        :return:
        :rtype: SymmetricState
        """
        return SymmetricState(cipherstate or self.create_cipherstate(), hash or self._hash)

    def create_handshakestate(self, symmetricstate=None, dh=None):
        """
        :param symmetricstate:
        :type symmetricstate: SymmetricState
        :param dh:
        :type dh: DH
        :return:
        :rtype: HandshakeState
        """
        return HandshakeState(symmetricstate or self.create_symmetricstate(), dh or self._dh)

[((56, 15, 56, 50), 'noise.processing.impl.cipherstate.CipherState', 'CipherState', ({(56, 27, 56, 49): '(cipher or self._cipher)'}, {}), '(cipher or self._cipher)', False, 'from noise.processing.impl.cipherstate import CipherState\n'), ((27, 27, 27, 80), 'noise.processing.handshakepatterns.handshakepattern.HandshakePattern.parse_handshakepattern', 'HandshakePattern.parse_handshakepattern', ({(27, 67, 27, 79): 'pattern.name'}, {}), '(pattern.name)', False, 'from noise.processing.handshakepatterns.handshakepattern import HandshakePattern\n')]