// Contract: MerkleTwoDrop
// Target function: function awardFromMany(uint[] calldata _ids, address _recipient, 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);
    }