contract SetOperator is Wallet, ReentrancyGuard {
    address signerOne;
    address signerTwo;

    address operator;

    event NewOperatorVote ();
    event AuthorizedOperator (address operator);
    event RevokedOperator (address operator);

    address[] public operators;

    bool public operatorVoteOne;
    bool public operatorVoteTwo;
    address public operatorVoteAddress;

    bool firstOperator = false;

   constructor (address _signerOne, address _signerTwo, address _tokenContract) public {
        signerOne = _signerOne;
        signerTwo = _signerTwo;
        tokenContract = IERC777(_tokenContract);
    }

    function getOperators () external view returns (address[] memory) {
        return operators;
    } //TODO: Check if works

    function setDefaultOperators(address[] memory _defaultOperators) external onlyOnce("assignFirstOperator") onlyDeployer {
        require(firstOperator == false, "Can only be ran once!");

        for (uint i = 0; i < _defaultOperators.length; i++) {
            _authorizeOperator(_defaultOperators[i]);
            emit AuthorizedOperator(_defaultOperators[i]);
        }

        firstOperator = true;
    }

    function revokeOperator (address _operator) isSigner external returns (address[] memory){
        uint i = 0;
        for (;i < operators.length; i++) {
            if (_operator == operators[i]) {
                break;
            }
            if (i == operators.length - 1) {
                revert("Address is not an operator");
            }
        }
        tokenContract.revokeOperator(_operator);
        _removeOperator(i);
        emit RevokedOperator(_operator);

        return operators;
    }

    function _removeOperator (uint index) private {
        if (index >= operators.length) {
             require(false, "index out of bounds");
        }

        for (uint i = index; i < operators.length-1; i++){
            operators[i] = operators[i+1];
        }
        operators.pop();
    }

    //TODO: revoke operator
    function _authorizeOperator (address _operator) private {
        tokenContract.authorizeOperator(_operator);
        operators.push(_operator);

        emit AuthorizedOperator(_operator);
    }

    function createOperatorVote (address _operator) external isSigner {
        operatorVoteAddress = _operator;
        operatorVoteOne = msg.sender == signerOne;
        operatorVoteTwo = msg.sender == signerTwo;

        emit NewOperatorVote();
    }

    function approveOperatorVote () external nonReentrant isSigner {
        if (msg.sender == signerOne) {
            operatorVoteOne = true;
        }
        if (msg.sender == signerTwo) {
            operatorVoteTwo = true;
        }

        if (operatorVoteOne == operatorVoteTwo) {
            _authorizeOperator(operatorVoteAddress);
        }
    }

    modifier isSigner () {
        require(msg.sender == signerOne || msg.sender == signerTwo, "Not allowed");
        _;
    }
}
function _authorizeOperator (address _operator) private {
    tokenContract.authorizeOperator(_operator);
    operators.push(_operator);

    emit AuthorizedOperator(_operator);
}

//TODO: revoke operator