// Contract: MerkleTwoDrop
// Target function: function awardToMany(uint _id, address[] calldata _recipients, uint[] calldata _amount0s, uint[] calldata _amount1s, bytes32[][] calldata _proofs) public
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract MerkleTwoDrop {

    struct Airdrop {
      bytes32 root;
      mapping(address => bool) awarded;
    }

    /// Events
    event Start(uint id);
    event Award(uint id, address recipient, uint amount0, uint amount1);

    /// State
    mapping(uint => Airdrop) public airdrops;
    ITokenManager public tokenManager0;
    ITokenManager public tokenManager1;
    uint public airdropsCount;
    address public startAuth;

    bool private initialized;

    function initialize(address _tokenManager0, address _tokenManager1, address _startAuth) public {
        require(!initialized, "Already initialized");
        initialized = true;

        require(_tokenManager0 != address(0), "Needs token0 manager");
        require(_tokenManager1 != address(0), "Needs token1 manager");
        require(_startAuth != address(0), "Needs startAuth");

        tokenManager0 = ITokenManager(_tokenManager0);
        tokenManager1 = ITokenManager(_tokenManager1);
        startAuth = _startAuth;
    }

    /**
     * @notice Start a new airdrop `_root`
     * @param _root New airdrop merkle root
     */
    function start(bytes32 _root) public {
        require(msg.sender == startAuth, "Not authorized");
        _start(_root);
    }

    function _start(bytes32 _root) internal returns(uint id){
        id = ++airdropsCount;    // start at 1
        Airdrop storage newAirdrop = airdrops[id];
        newAirdrop.root = _root;
        emit Start(id);
    }

    /**
     * @notice Award from airdrop
     * @param _id Airdrop id
     * @param _recipient Recepient of award
     * @param _amount0 The token0 amount
     * @param _amount1 The token1 amount
     * @param _proof Merkle proof to correspond to data supplied
     */
    function award(uint _id, address _recipient, uint256 _amount0, uint256 _amount1, bytes32[] calldata _proof) public {
        Airdrop storage airdrop = airdrops[_id];

        bytes32 hash = keccak256(abi.encodePacked(_recipient, _amount0, _amount1));
        require( validate(airdrop.root, _proof, hash), "Invalid proof" );

        require( !airdrops[_id].awarded[_recipient], "Already awarded" );

        airdrops[_id].awarded[_recipient] = true;

        tokenManager0.mint(_recipient, _amount0);
        tokenManager1.mint(_recipient, _amount1);

        emit Award(_id, _recipient, _amount0, _amount1);
    }

    /**
     * @notice Award from multiple airdrops to single recipient
     * @param _ids Airdrop ids
     * @param _recipient Recepient of award
     * @param _amount0s The token0 amounts
     * @param _amount1s The token1 amounts
     * @param _proofs Merkle proofs
     */
    function awardFromMany(uint[] calldata _ids, address _recipient, uint[] calldata _amount0s, uint[] calldata _amount1s, bytes32[][] calldata _proofs) public {

        uint totalAmount0;
        uint totalAmount1;

        for (uint i = 0; i < _ids.length; i++) {
            uint id = _ids[i];

            bytes32 hash = keccak256(abi.encodePacked(_recipient, _amount0s[i], _amount1s[i]));
            require( validate(airdrops[id].root, _proofs[i], hash), "Invalid proof" );

            require( !airdrops[id].awarded[_recipient], "Already awarded" );

            airdrops[id].awarded[_recipient] = true;

            totalAmount0 += _amount0s[i];
            totalAmount1 += _amount1s[i];

            emit Award(id, _recipient, _amount0s[i], _amount1s[i]);
        }

        tokenManager0.mint(_recipient, totalAmount0);
        tokenManager1.mint(_recipient, totalAmount1);

    }