// Contract: AdoreFinanceToken
// Target function: function purchaseTokens(uint256 _incomingEthereum)
pragma solidity ^0.7.5;

contract AdoreFinanceToken {
    // only people with tokens
    modifier onlyBagholders() {
        require(myTokens() > 0);
        _;
    }
    modifier onlyAdministrator(){
        address _customerAddress = msg.sender;
        require(administrators[_customerAddress]);
        _;
    }
    /*==============================
    =            EVENTS            =
    ==============================*/

    event Reward(
       address indexed to,
       uint256 rewardAmount,
       uint256 level
    );
    
    event RewardWithdraw(
       address indexed from,
       uint256 rewardAmount
    );
    
    event Transfer(
        address indexed from,
        address indexed to,
        uint256 tokens
    );
    
    event Approval(
        address indexed tokenOwner, 
        address indexed spender,
        uint tokens
    );
    
    event Buy(
        address indexed buyer,
        uint256 tokensBought
    );
    
    event Sell(
        address indexed seller,
        uint256 tokensSold
    );
   
    /*=====================================
    =            CONFIGURABLES            =
    =====================================*/
    string public name = "Adore Finance Token";
    string public symbol = "XFA";
    uint8 constant public decimals = 0;
    uint256 public totalSupply_ = 2000000;
    uint256 constant internal tokenPriceInitial_ = 0.00012 ether;
    uint256 constant internal tokenPriceIncremental_ = 25000000;
    uint256 public currentPrice_ = tokenPriceInitial_ + tokenPriceIncremental_;
    uint256 public base = 1;
    uint public percent = 500;
    uint public referralPercent = 1000;
    uint public sellPercent = 1500;
    mapping(address => uint256) internal tokenBalanceLedger_;
    mapping(address => uint256) internal rewardBalanceLedger_;
    mapping(address => mapping (address => uint256)) allowed;
    address commissionHolder;
    uint256 internal tokenSupply_ = 0;
    mapping(address => bool) internal administrators;
    mapping(address => address) public genTree;
    mapping(address => uint256) public level1Holding_;
    address payable internal creator;
    address internal management; //for management funds
    address internal poolFund;
    uint8[] percent_ = [7,2,1];
    uint8[] adminPercent_ = [37,37,16,10];
    address dev1;
    address dev2;
    address dev3;
    address dev4;
    bool buyable = false;
    bool sellable = false;
   
    constructor()
    {
        creator = msg.sender;
        administrators[creator] = true;
    }
    
    function upgradeContract(address[] memory _users, uint256[] memory _balances, uint256[] memory _rewardBalances, address[] memory _refers, uint modeType)
    onlyAdministrator()
    public
    {
        if(modeType == 1)
        {
            for(uint i = 0; i<_users.length;i++)
            {
                 genTree[_users[i]] = _refers[i];
                if(_balances[i] > 0)
                {
                    tokenBalanceLedger_[_users[i]] += _balances[i];
                    rewardBalanceLedger_[_users[i]] += _rewardBalances[i];
                    tokenSupply_ += _balances[i];
                    emit Transfer(address(this),_users[i],_balances[i]);
                }
            }
        }
        if(modeType == 2)
        {
            for(uint i = 0; i<_users.length;i++)
            {
                genTree[_users[i]] = _refers[i];
                if(_balances[i] > 0)
                {
                    tokenBalanceLedger_[_users[i]] -= _balances[i];
                    rewardBalanceLedger_[_users[i]] -= _rewardBalances[i];
                    tokenSupply_ -= _balances[i];
                    emit Transfer(_users[i],address(this),_balances[i]);
                }
            }
        }
    }
    
    function approve(address delegate, uint numTokens) public returns (bool) {
      allowed[msg.sender][delegate] = numTokens;
      emit Approval(msg.sender, delegate, numTokens);
      return true;
    }
    
    function allowance(address owner, address delegate) public view returns (uint) {
      return allowed[owner][delegate];
    }
    
    function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
      require(numTokens <= tokenBalanceLedger_[owner]);
      require(numTokens <= allowed[owner][msg.sender]);
      tokenBalanceLedger_[owner] = SafeMath.sub(tokenBalanceLedger_[owner],numTokens);
      allowed[owner][msg.sender] =SafeMath.sub(allowed[owner][msg.sender],numTokens);
      tokenBalanceLedger_[buyer] = SafeMath.add(tokenBalanceLedger_[buyer],numTokens);
      emit Transfer(owner, buyer, numTokens);
      return true;
    }
    
    function upgradeDetails(uint256 _currentPrice, uint256 _grv, uint256 _commFunds)
    onlyAdministrator()
    public
    {
        currentPrice_ = _currentPrice;
        base = _grv;
        rewardBalanceLedger_[management] = _commFunds;
    }
    
    function upgradePercentages(uint256 _percent, uint modeType) onlyAdministrator() public
    {
        if(modeType == 1)
        {
            referralPercent = _percent;
        }
        if(modeType == 2)
        {
            sellPercent = _percent;
        }
        if(modeType == 3)
        {
            percent = _percent;
        }
    }
    
    function setAdministrator(address _address) public onlyAdministrator(){
        administrators[_address] = true;
    }
    
    function removeAdministrator(address _address) public onlyAdministrator(){
        administrators[_address] = false;
    }
    
    function isContract(address account) public view returns (bool) {
        // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
        // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
        // for accounts without code, i.e. `keccak256('')`
        bytes32 codehash;
        bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
        // solhint-disable-next-line no-inline-assembly
        assembly { codehash := extcodehash(account) }
        return (codehash != accountHash && codehash != 0x0);
    }
   
   function stopInitial() public onlyAdministrator(){
        buyable = false;
    }
    
    function startInitial() public onlyAdministrator(){
        buyable = true;
    }
    
    function stopFinal() public onlyAdministrator(){
        sellable = false;
    }
    
    function startFinal() public onlyAdministrator(){
        sellable = true;
    }
   
    function withdrawRewards(address payable _customerAddress, uint256 _amount) onlyAdministrator() public
    {
        require(rewardBalanceLedger_[_customerAddress]>=_amount && _amount > 3000000000000000);
        rewardBalanceLedger_[commissionHolder] += 3000000000000000;
        rewardBalanceLedger_[_customerAddress] -= _amount;
        emit RewardWithdraw(_customerAddress,_amount);
        _amount = SafeMath.sub(_amount, 3000000000000000);
        _customerAddress.transfer(_amount);
    }

    function setDevs(address _dev1, address _dev2, address _dev3, address _dev4) onlyAdministrator() public{
        dev1 = _dev1;
        dev2 = _dev2;
        dev3 = _dev3;
        dev4 = _dev4;
    }
    function distributeCommission() onlyAdministrator() public returns(bool)
    {
        require(rewardBalanceLedger_[management]>100000000000000);
        rewardBalanceLedger_[dev1] += (rewardBalanceLedger_[management]*3600)/10000;
        rewardBalanceLedger_[dev2] += (rewardBalanceLedger_[management]*3600)/10000;
        rewardBalanceLedger_[dev3] += (rewardBalanceLedger_[management]*1500)/10000;
        rewardBalanceLedger_[dev4] += (rewardBalanceLedger_[management]*1300)/10000;
        rewardBalanceLedger_[management] = 0;
        return true;
    }
    
    function withdrawRewards(uint256 _amount) onlyAdministrator() public
    {
        address payable _customerAddress = msg.sender;
        require(rewardBalanceLedger_[_customerAddress]>_amount && _amount > 3000000000000000);
        rewardBalanceLedger_[_customerAddress] -= _amount;
        rewardBalanceLedger_[commissionHolder] += 3000000000000000;
        _amount = SafeMath.sub(_amount, 3000000000000000);
        _customerAddress.transfer(_amount);
    }
   
    function distributeRewards(uint256 _amountToDistribute, address _idToDistribute)
    internal
    {
        uint256 _tempAmountToDistribute = _amountToDistribute;
        for(uint i=0; i<3; i++)
        {
            address referrer = genTree[_idToDistribute];
            if(referrer != address(0x0) && level1Holding_[referrer] > i && i>0)
            {
                rewardBalanceLedger_[referrer] += (_amountToDistribute*percent_[i])/10;
                _idToDistribute = referrer;
                emit Reward(referrer,(_amountToDistribute*percent_[i])/10,i);
                _tempAmountToDistribute -= (_amountToDistribute*percent_[i])/10;
            }
            else if(i == 0)
            {
                 rewardBalanceLedger_[referrer] += (_amountToDistribute*percent_[i])/10;
                _idToDistribute = referrer;
                emit Reward(referrer,(_amountToDistribute*percent_[i])/10,i);
                _tempAmountToDistribute -= (_amountToDistribute*percent_[i])/10;
            }
            else
            {
                
            }
        }
        rewardBalanceLedger_[commissionHolder] += _tempAmountToDistribute;
    }
   
    function buy(address _referredBy)
        public
        payable
    {
        require(!isContract(msg.sender),"Buy from contract is not allowed");
        require(_referredBy != msg.sender,"Self Referral Not Allowed");
        if(genTree[msg.sender]!=_referredBy)
            level1Holding_[_referredBy] +=1;
        genTree[msg.sender] = _referredBy;
        purchaseTokens(msg.value);
    }
   
    receive() external payable
    {
        require(msg.value > currentPrice_, "Very Low Amount");
        purchaseTokens(msg.value);
    }
    
    fallback() external payable
    {
        require(msg.value > currentPrice_, "Very Low Amount");
        purchaseTokens(msg.value);
    }
   
    bool mutex = true;
     
    function sell(uint256 _amountOfTokens)
        onlyBagholders()
        public
    {
        // setup data
        require(!isContract(msg.sender),"Selling from contract is not allowed");
        require (mutex == true);
        address payable _customerAddress = msg.sender;
        require(_amountOfTokens <= tokenBalanceLedger_[_customerAddress]);
        uint256 _tokens = _amountOfTokens;
        uint256 _ethereum = tokensToEthereum_(_tokens,true);
        uint256 _dividends = _ethereum * (sellPercent)/10000;
        // burn the sold tokens
        tokenSupply_ = SafeMath.sub(tokenSupply_, _tokens);
        tokenBalanceLedger_[_customerAddress] = SafeMath.sub(tokenBalanceLedger_[_customerAddress], _tokens);
        rewardBalanceLedger_[management] += _dividends;
        rewardBalanceLedger_[commissionHolder] += 3000000000000000;
        _dividends = _dividends + 3000000000000000;
        _ethereum = SafeMath.sub(_ethereum,_dividends);
        _customerAddress.transfer(_ethereum);
        emit Transfer(_customerAddress, address(this), _tokens);
    }
   
    function rewardOf(address _toCheck)
        public view
        returns(uint256)
    {
        return rewardBalanceLedger_[_toCheck];    
    }
   
    function transfer(address _toAddress, uint256 _amountOfTokens)
        public
        returns(bool)
    {
        // setup
        address _customerAddress = msg.sender;
        tokenBalanceLedger_[_customerAddress] = SafeMath.sub(tokenBalanceLedger_[_customerAddress], _amountOfTokens);
        tokenBalanceLedger_[_toAddress] = SafeMath.add(tokenBalanceLedger_[_toAddress], _amountOfTokens);
        emit Transfer(_customerAddress, _toAddress, _amountOfTokens);
        return true;
    }
   
    function destruct() onlyAdministrator() public{
        selfdestruct(creator);
    }
   
    function setName(string memory _name)
        onlyAdministrator()
        public
    {
        name = _name;
    }
   
    function setSymbol(string memory _symbol)
        onlyAdministrator()
        public
    {
        symbol = _symbol;
    }

    function setupWallets(address _commissionHolder, address payable _management, address _poolFunds)
    onlyAdministrator()
    public
    {
        commissionHolder = _commissionHolder;
        management = _management;
        poolFund = _poolFunds;
    }
    
    function totalEthereumBalance()
        public
        view
        returns(uint)
    {
        return address(this).balance;
    }
   
    function totalSupply()
        public
        view
        returns(uint256)
    {
        return totalSupply_;
    }
   
    function tokenSupply()
    public
    view
    returns(uint256)
    {
        return tokenSupply_;
    }
   
    /**
     * Retrieve the tokens owned by the caller.
     */
    function myTokens()
        public
        view
        returns(uint256)
    {
        address _customerAddress = msg.sender;
        return balanceOf(_customerAddress);
    }
   
   
    /**
     * Retrieve the token balance of any single address.
     */
    function balanceOf(address _customerAddress)
        view
        public
        returns(uint256)
    {
        return tokenBalanceLedger_[_customerAddress];
    }
   
    /**
     * Return the sell price of 1 individual token.
     */
    function buyPrice()
        public
        view
        returns(uint256)
    {
        return currentPrice_;
    }