contract DRCCrowSale is SafeMath,DSAuth {
    DRCToken public DRC;

    // Constants
    uint256 public constant tokensPerEth = 10000;// DRC per ETH 
    uint256 public presalePerEth;// DRC per ETH 
    
    uint256 public constant totalSupply = 1 * 1e9 * 1e18; // Total DRC amount created

    uint256 public tokensForTeam     = totalSupply * 15 / 100;
    uint256 public tokensForParnter  = totalSupply * 15 / 100;
    uint256 public tokensForPlatform = totalSupply * 45 / 100;

    uint256 public tokensForPresale1 = totalSupply * 5 / 100;
    uint256 public tokensForPresale2 = totalSupply * 10 / 100;
    uint256 public tokensForSale     = totalSupply * 10 / 100;
    
    address public team;
    address public parnter;
    address public platform;
    address public presale1;
    
    uint256 public Presale1Sold = 0;
    uint256 public Presale2Sold = 0;
    uint256 public PublicSold = 0;

    enum IcoState {Init,Presale1, Presale2, Running, Paused, Finished}
    IcoState public icoState = IcoState.Init;
    IcoState public preIcoState = IcoState.Init;
    
    function setPresalePerEth(uint256 discount) external Auth{
        presalePerEth = discount;
    }

    function startPreSale1() external Auth {
        require(icoState == IcoState.Init);
        icoState = IcoState.Presale1;
    }

    function startPreSale2() external Auth {
        require(icoState == IcoState.Presale1);
        icoState = IcoState.Presale2;
    }

    function startIco() external Auth {
        require(icoState == IcoState.Presale2);
        icoState = IcoState.Running;
    }

    function pauseIco() external Auth {
        require(icoState != IcoState.Paused);
        preIcoState = icoState ;
        icoState = IcoState.Paused;
    }

    function continueIco() external Auth {
        require(icoState == IcoState.Paused);
        icoState = preIcoState;
    }
    
    uint public finishTime = 0;
    function finishIco() external Auth {
        require(icoState == IcoState.Running);
        icoState = IcoState.Finished;
        finishTime = block.timestamp;
    }
    
    uint public unfreezeStartTime = 0;
    function setUnfreezeStartTime(uint timestamp) external Auth{
        unfreezeStartTime = timestamp;
    }
    
    mapping (uint => mapping (address => bool))  public  unfroze;
    mapping (address => uint256)                 public  userBuys;
    mapping (uint => bool)  public  burned;
    
    // anyone can burn
    function burn(IcoState state) external Auth{
        uint256 burnAmount = 0;
        //only burn once
        assert(burned[uint(state)] == false);
        if(state == IcoState.Presale1 && (icoState == IcoState.Presale2 || icoState == IcoState.Finished)){
            assert(Presale1Sold < tokensForPresale1);
            burnAmount = safeSub(tokensForPresale1,Presale1Sold);
        } 
        else if(state == IcoState.Presale2 && icoState == IcoState.Finished){ 
            assert(Presale2Sold < tokensForPresale2);
            burnAmount = safeSub(tokensForPresale2,Presale2Sold);
        } 
        else if(state == IcoState.Finished && icoState == IcoState.Finished){
            assert(PublicSold < tokensForSale);
            burnAmount = safeSub(tokensForSale,PublicSold);
        } 
        else {
            throw;
        }

        DRC.burn(burnAmount);
        burned[uint(state)] = true;
    }
        
    function presaleUnfreeze(uint step) external{
        
        assert(unfroze[step][msg.sender] == false);
        assert(DRC.freezeOf(msg.sender) > 0 );
        assert(unfreezeStartTime > 0);
        assert(msg.sender != platform);
        
        uint256 freeze  = DRC.freezeOf(msg.sender);
        uint256 unfreezeAmount = 0;

        if(step == 1){
            require( block.timestamp > (unfreezeStartTime + 30 days));
            unfreezeAmount = freeze / 3;
        }
        else if(step == 2){
            require( block.timestamp > (unfreezeStartTime + 60 days));
            unfreezeAmount = freeze / 2;
        }
        else if(step == 3){
            require( block.timestamp > (unfreezeStartTime + 90 days));
            unfreezeAmount = freeze;
        }
        else{
            throw ;
        }
        
        require(unfreezeAmount > 0 );
        
        DRC.unfreeze(msg.sender,unfreezeAmount);
        unfroze[step][msg.sender] = true;
    }
    
    //team unfreeze
    function teamUnfreeze() external{
        uint month = 6;
        
        assert(DRC.freezeOf(msg.sender) > 0 );
        assert(finishTime > 0);
        assert(msg.sender == team);
        uint step = safeSub(block.timestamp, finishTime) / (3600*24*30);
        
        uint256 freeze  = DRC.freezeOf(msg.sender);
        uint256 unfreezeAmount = 0;
        
        uint256 per = tokensForTeam / month;
        
        for(uint i = 0 ;i <= step && i < month;i++){
            if(unfroze[i][msg.sender] == false){
                unfreezeAmount += per;
            }
        }
        
        require(unfreezeAmount > 0 );
        require(unfreezeAmount <= freeze);

        DRC.unfreeze(msg.sender,unfreezeAmount);
        for(uint j = 0; j <= step && i < month; j++){
            unfroze[j][msg.sender] = true;
        }
    }
    
    //platform unfreeze
     function platformUnfreeze() external{
        uint month = 12;
        
        assert(DRC.freezeOf(msg.sender) > 0 );
        assert(finishTime > 0);
        assert(msg.sender == platform);
        uint step = safeSub(block.timestamp, finishTime) / (3600*24*30);
        
        uint256 freeze  = DRC.freezeOf(msg.sender);
        uint256 unfreezeAmount = 0;
        
        uint256 per = tokensForPlatform / month;
        
        for(uint i = 0 ;i <= step && i < month;i++){
            if(unfroze[i][msg.sender] == false){
                unfreezeAmount += per;
            }
        }
        
        require(unfreezeAmount > 0 );
        require(unfreezeAmount <= freeze);

        DRC.unfreeze(msg.sender,unfreezeAmount);
        for(uint j = 0; j <= step && i < month; j++){
            unfroze[j][msg.sender] = true;
        }
    }
    
    // Constructor
    function DRCCrowSale() public {

    }

    function initialize(DRCToken drc,address _team,address _parnter,address _platform,address _presale1) Auth public {
        assert(address(DRC) == address(0));
        assert(drc.owner() == address(this));
        assert(drc.totalSupply() == 0);
        assert(_team != _parnter && _parnter != _platform && _team != _platform);
        
        team =_team;
        parnter=_parnter;
        platform=_platform;
        presale1 = _presale1;

        DRC = drc;
        DRC.mint(totalSupply);
        
        // transfer to team partner platform 
        DRC.push(team, tokensForTeam);
        DRC.freeze(team,tokensForTeam);
        
        DRC.push(parnter, tokensForParnter);
        
        // freeze
        DRC.push(platform, tokensForPlatform);
        DRC.freeze(platform,tokensForPlatform);
        
        DRC.push(presale1, tokensForPresale1);
        
    }

    function() payable public {
        buy();
    }

    function buy()  payable public{
        require( (icoState == IcoState.Running)  ||
                 (icoState == IcoState.Presale1) || 
                 (icoState == IcoState.Presale2) );
        // require          
        if((icoState == IcoState.Presale1) || (icoState == IcoState.Presale2)){
            require(msg.value >= 10 ether);
        } 
        else {
            require(msg.value >= 0.01 ether);
            //limit peer user less than 10 eth
            require(userBuys[msg.sender] + msg.value <= 10 ether);
        }
 

        uint256 amount = getDRCTotal(msg.value);
        uint256 sold = 0;
        uint256 canbuy = 0;
        (sold,canbuy) = getSold();

        // refund eth for last buy
        if (sold + amount > canbuy){
            uint256 delta = sold + amount - canbuy;
            uint256 refundMoney = msg.value * delta / amount;
            amount = canbuy-sold;
            require(refundMoney > 0);
            msg.sender.transfer(refundMoney);
        }
        
        require(amount > 0);
    
        DRC.push(msg.sender, amount);
        
        //presale auto freeze
        if((icoState == IcoState.Presale1)  || (icoState == IcoState.Presale2)){
            DRC.freeze(msg.sender,amount);
        }
        else{
            //for limit amount peer user 
            userBuys[msg.sender] += amount;
        }
        
       addSold(amount);
    }
    
    function getSold() private view returns ( uint256,uint256){
        if(icoState == IcoState.Presale1){
            return(Presale1Sold,tokensForPresale1);
        } 
        else if(icoState == IcoState.Presale2){
            return(Presale2Sold,tokensForPresale2);
        } 
        else if(icoState == IcoState.Running){
            return(PublicSold,tokensForSale);
        }else{
            throw;
        }
    }

    function addSold(uint256 amount) private{
        if(icoState == IcoState.Presale1){
            Presale1Sold += amount;
        } 
        else if(icoState == IcoState.Presale2){
            Presale2Sold += amount;
        } 
        else if(icoState == IcoState.Running){
            PublicSold += amount;
        }
        else{
            throw;
        }
    }
    
    //discount
    function getDRCTotal(uint256 _eth) public view returns ( uint256)
    {
        if(icoState == IcoState.Presale1)
        {
            return safeMult(_eth , presalePerEth);
        }
        else if(icoState == IcoState.Presale2)
        {
           return safeMult(_eth , presalePerEth);
        }

        return safeMult(_eth , tokensPerEth);
    }

    function finalize() external Owner payable {
        require(this.balance > 0 );

        require(owner.send(this.balance));
    }

}
function platformUnfreeze() external{
        uint month = 12;
        
        assert(DRC.freezeOf(msg.sender) > 0 );
        assert(finishTime > 0);
        assert(msg.sender == platform);
        uint step = safeSub(block.timestamp, finishTime) / (3600*24*30);
        
        uint256 freeze  = DRC.freezeOf(msg.sender);
        uint256 unfreezeAmount = 0;
        
        uint256 per = tokensForPlatform / month;
        
        for(uint i = 0 ;i <= step && i < month;i++){
            if(unfroze[i][msg.sender] == false){
                unfreezeAmount += per;
            }
        }
        
        require(unfreezeAmount > 0 );
        require(unfreezeAmount <= freeze);

        DRC.unfreeze(msg.sender,unfreezeAmount);
        for(uint j = 0; j <= step && i < month; j++){
            unfroze[j][msg.sender] = true;
        }
    }
//platform unfreeze