// File: contracts/TeamRevShare.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Splitter {
    uint256 public constant PRECISION = 100_00;

    // Addresses for distribution
    address public ZKitty;
    address public ClipFinance;

    // Proportions
    uint256 public currentZKittyProportion;
    uint256 public currentClipFinanceProportion;

    // For multi-signature proportion change
    uint256 public customZKittyProportion;
    uint256 public customClipFinanceProportion;

    // Basic Multisig
    bool public ZKittyAgree;
    bool public ClipFinanceAgree;

    constructor(
        address _ZKitty,
        address _ClipFinance,
        uint256 _ZKittyProportion
    ) {
        ZKitty = _ZKitty;
        ClipFinance = _ClipFinance;

        currentZKittyProportion = _ZKittyProportion;
        currentClipFinanceProportion = PRECISION - _ZKittyProportion;
    }

    function setAddress(address _newAddress) external {
        if (msg.sender == ZKitty) {
            ZKitty = _newAddress;
        } else if (msg.sender == ClipFinance) {
            ClipFinance = _newAddress;
        } else {
            revert("Not authorized");
        }
    }

    function activatePhaseTwo() external {
        require(
            msg.sender == ZKitty || msg.sender == ClipFinance,
            "Not Allowed!"
        );
        currentZKittyProportion = 6000;
        currentClipFinanceProportion = 4000;
    }

    function setCustomFee(
        uint256 _customZKittyProportion,
        uint256 _customClipFinanceProportion
    ) external {
        require(
            msg.sender == ZKitty || msg.sender == ClipFinance,
            "Not Allowed!"
        );
        require(
            _customZKittyProportion + _customClipFinanceProportion <= PRECISION,
            "Invalid proportion"
        );
        ZKittyAgree = false;
        ClipFinanceAgree = false;
        customZKittyProportion = _customZKittyProportion;
        customClipFinanceProportion = _customClipFinanceProportion;
    }

    function approveChange() external {
        if (msg.sender == ZKitty) {
            ZKittyAgree = true;
        } else if (msg.sender == ClipFinance) {
            ClipFinanceAgree = true;
        } else {
            revert("Not authorized");
        }
        if (ZKittyAgree == true && ClipFinanceAgree == true) {
            ZKittyAgree = false;
            ClipFinanceAgree = false;
            currentZKittyProportion = customZKittyProportion;
            customClipFinanceProportion = customClipFinanceProportion;
        }
    }

    function distribute() public {
        uint256 balance = address(this).balance;

        uint256 ZKittyAmount = (balance * currentZKittyProportion) / PRECISION;
        uint256 ClipFinanceAmount = balance - ZKittyAmount;

        // Sending to ZKitty
        (bool successZKitty, ) = ZKitty.call{value: ZKittyAmount}("");
        require(successZKitty, "Transfer to ZKitty failed");

        // Sending to ClipFinance
        (bool successClipFinance, ) = ClipFinance.call{
            value: ClipFinanceAmount
        }("");
        require(successClipFinance, "Transfer to ClipFinance failed");
    }

    // Fallback and receive functions
    fallback() external payable {
        distribute();
    }

    receive() external payable {
        distribute();
    }
}