contract PolicyRegistry is Ownable, EmergencySafe, Upgradeable{

  event PolicyCreated(address at, address by);

  IXTPaymentContract public IXTPayment;

  mapping (address => address[]) private policiesByParticipant;
  address[] private policies;


  /**
   * @dev Creates Registry
   * @param paymentAddress The address of the payment contract used when creating insurance contracts
   */
  function PolicyRegistry(address paymentAddress) public {
    IXTPayment = IXTPaymentContract(paymentAddress);
  }

  /**
   * @dev Creates Policy, transfers ownership to msg.sender, registers address for all parties involved,
   * and transfers IXT 
   */
  function createContract(string _clientInfo, address _brokerEtherAddress, address _clientEtherAddress, string _enquiryId) public isNotPaused {

    Policy policy = new Policy(_clientInfo, _brokerEtherAddress, _clientEtherAddress, _enquiryId);
    policy.changeOwner(msg.sender);
    policiesByParticipant[_brokerEtherAddress].push(policy);

    if (_clientEtherAddress != _brokerEtherAddress) {
      policiesByParticipant[_clientEtherAddress].push(policy);
    }

    if (msg.sender != _clientEtherAddress && msg.sender != _brokerEtherAddress) {
      policiesByParticipant[msg.sender].push(policy);
    }

    policies.push(policy);

    IXTPayment.transferIXT(_clientEtherAddress, owner, "create_insurance");
    emit PolicyCreated(policy, msg.sender);
  }

  /**
   * @dev Retrieve all contracts that msg.sender is either broker, client or owner for
   */
  function getMyPolicies() public view returns (address[]) {
    return policiesByParticipant[msg.sender];
  }

  /**
   * @dev Retrieve all contracts ever created
   */
  function getAllPolicies() public view ownerOnly returns (address[]){
    return policies;
  }

  /**
   * @dev change address of payment contract
   * @param contractAddress Address of payment contract
   */
  function changePaymentContract(address contractAddress) public ownerOnly{
    IXTPayment = IXTPaymentContract(contractAddress);
  }
}
/*
 * @title Policy Registry 
 * @dev Registry that is in charge of tracking and creating insurance contracts
 */
function getAllPolicies() public view ownerOnly returns (address[]){
    return policies;
  }
/**
 * @dev Retrieve all contracts ever created
 */