contract FeedToken is  ProductsInterface,OrderInterface, ERC20Interface,Ownable,Buyers {



   using SafeMath for uint256;
   //------------------------------------------------------------------------
    uint256 public counter=0;
    mapping (uint => Product) public seller_products;
    mapping (uint => uint) public products_price;
    mapping (address=> uint) public seller_total_products;
   //------------------------------------------------------------------------
   string public name;
   string public symbol;
   uint256 public decimals;

   uint256 public _totalSupply;
   uint256 order_counter=0;
   mapping(address => uint256) tokenBalances;
   address ownerWallet;
   // Owner of account approves the transfer of an amount to another account
   mapping (address => mapping (address => uint256)) allowed;
   
   mapping (address=>uint) privateSaleBuyerTokens;
   address[] privateSaleBuyers;
   
   mapping (address=>uint) preSaleBuyerTokens;
   address[] preSaleBuyers;
   
   mapping(address=>uint) teamMembers;
   
   uint privateSaleEndDate;
   uint preSaleEndDate;
   uint icoEndDate;
   /**
   * @dev Contructor that gives msg.sender all of existing tokens.
   */
    constructor(address wallet) public {
        owner = wallet;
        name  = "Feed";
        symbol = "FEED";
        decimals = 18;
        _totalSupply = 1000000000 * 10 ** uint(decimals);
        tokenBalances[ msg.sender] = _totalSupply;   //Since we divided the token into 10^18 parts
    }
    
     // Get the token balance for account `tokenOwner`
     function balanceOf(address tokenOwner) public constant returns (uint balance) {
         return tokenBalances[tokenOwner];
     }
  
     // Transfer the balance from owner's account to another account
     function transfer(address to, uint tokens) public returns (bool success) {
         require(to != address(0));
         require(tokens <= tokenBalances[msg.sender]);
         checkTokenVesting(msg.sender, tokens);
         tokenBalances[msg.sender] = tokenBalances[msg.sender].sub(tokens);
         tokenBalances[to] = tokenBalances[to].add(tokens);
         emit Transfer(msg.sender, to, tokens);
         return true;
     }
     function checkUser() public constant returns(string ){
         require(bytes(registerbuyer[msg.sender].name).length!=0);
          
             return "Register User";
     }
     
     function checkTokenVesting(address sender, uint tokens) internal {
         uint lockupTime;
         uint daysPassedSinceEndDate;
         uint lockedTokens;
         if (preSaleBuyerTokens[sender] > 0 || privateSaleBuyerTokens[sender]>0 || teamMembers[sender]>0)
         {
             if (teamMembers[sender]>0)
             {
                lockupTime = uint(24).mul(uint(30)).mul(1 days);
                if (now<icoEndDate.add(lockupTime))
                {
                    lockedTokens = teamMembers[sender];
                    if (lockedTokens.add(tokens)>tokenBalances[sender])
                        revert();
                }   
             }
             else if (privateSaleBuyerTokens[sender]>0)
             {
                lockupTime = uint(12).mul(uint(30)).mul(1 days);
                uint daysPassedSincePrivateSaleEnd = now.sub(privateSaleEndDate);
                daysPassedSincePrivateSaleEnd = daysPassedSincePrivateSaleEnd.div(1 days);
                uint monthsPassedSinceICOEnd = daysPassedSincePrivateSaleEnd.div(30);
                uint unlockedTokens = privateSaleBuyerTokens[sender].div(12).mul(monthsPassedSinceICOEnd);
                lockedTokens = privateSaleBuyerTokens[sender].sub(unlockedTokens);
                if (lockedTokens.add(tokens)>tokenBalances[sender])
                    revert();
                
             }
             else if (preSaleBuyerTokens[sender]>0)
             {
               lockupTime = uint(3).mul(uint(30)).mul(1 days);
               if (now<preSaleEndDate.add(lockupTime))
                {
                    lockedTokens = preSaleBuyerTokens[sender];
                    if (lockedTokens.add(tokens)>tokenBalances[sender])
                        revert();
                }   
             }
         }
     }
     /**
   * @dev Transfer tokens from one address to another
   * @param _from address The address which you want to send tokens from
   * @param _to address The address which you want to transfer to
   * @param _value uint256 the amount of tokens to be transferred
   */
   
  function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= tokenBalances[_from]);
    require(_value <= allowed[_from][msg.sender]);
    checkTokenVesting(_from,_value);
    tokenBalances[_from] = tokenBalances[_from].sub(_value);
    tokenBalances[_to] = tokenBalances[_to].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    emit Transfer(_from, _to, _value);
    return true;
  }
  
     /**
   * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
   *
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint256 _value) public returns (bool) {
    allowed[msg.sender][_spender] = _value;
    emit Approval(msg.sender, _spender, _value);
    return true;
  }

     // ------------------------------------------------------------------------
     // Total supply
     // ------------------------------------------------------------------------
     function totalSupply() public constant returns (uint) {
         return _totalSupply  - tokenBalances[address(0)];
     }
     
    
     
     // ------------------------------------------------------------------------
     // Returns the amount of tokens approved by the owner that can be
     // transferred to the spender's account
     // ------------------------------------------------------------------------
     function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
         return allowed[tokenOwner][spender];
     }
     
     /**
   * @dev Increase the amount of tokens that an owner allowed to a spender.
   *
   * @param _spender The address which will spend the funds.
   * @param _addedValue The amount of tokens to increase the allowance by.
   */
  function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
    allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

  /**
   * @dev Decrease the amount of tokens that an owner allowed to a spender.
   *
   * @param _spender The address which will spend the funds.
   * @param _subtractedValue The amount of tokens to decrease the allowance by.
   */
  function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
    uint oldValue = allowed[msg.sender][_spender];
    if (_subtractedValue > oldValue) {
      allowed[msg.sender][_spender] = 0;
    } else {
      allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
    }
    emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
    return true;
  }

     
     // ------------------------------------------------------------------------
     // Don't accept ETH
     // ------------------------------------------------------------------------
     function () public payable {
         revert();
     }
 
 
     // ------------------------------------------------------------------------
     // Owner can transfer out any accidentally sent ERC20 tokens
     // ------------------------------------------------------------------------
     function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
         return ERC20Interface(tokenAddress).transfer(owner, tokens);
     }
     
    
     function placeorder( uint256   quantity,uint256   product_index)  public  returns(uint256) {
         
        require(counter>=product_index && product_index>0);
        require(bytes(registerbuyer[msg.sender].name).length!=0);//to place order you first register yourself
         
        transfer(seller_products[product_index]._seller,seller_products[product_index].price*quantity);
        orders[order_counter] = Order(order_counter,quantity,product_index,seller_products[product_index].price, msg.sender,seller_products[product_index]._seller,0);
        
        emit OrderPlace(order_counter,quantity, product_index,  seller_products[product_index].name, msg.sender, seller_products[product_index]._seller );
        order_counter++;
        return counter;
    }
    
    //------------------------------------------------------------------------
    // product methods
    //------------------------------------------------------------------------
   
   
    function addproduct(string _name,string _image,uint256 _price,string _detail)   public   returns (bool success){
          require(bytes(_name).length!=0 &&
             bytes(_image).length!=0 &&
             bytes(_detail).length!=0 
            
             
        );
        counter++;
        seller_products[counter] = Product(counter,_name,_image, _price,_detail,msg.sender);
        products_price[counter]=_price;
        emit ProductAdded(counter,msg.sender,_name,_image,_price,_detail);
        return true;
   }
  
   function updateprice(uint _index, uint _price) public returns (bool success){
      require(seller_products[_index]._seller==msg.sender);
       
     
      seller_products[_index].price=_price;
      products_price[_index]=_price;
      return true;
  }
  
   function getproduuct(uint _index) public constant returns(uint256 ,string ,string ,uint256  ,string , address )
   {
       return(seller_products[_index].id,seller_products[_index].name,seller_products[_index].image,products_price[_index],seller_products[_index].detail,seller_products[_index]._seller);
   }
   function getproductprices() public constant returns(uint256[])
   {
       uint256[] memory price = new uint256[](counter);
        
        for (uint i = 0; i <counter; i++) {
           
            price[i]=products_price[i+1];
             
        }
      return price;
   }
    
    //------------------------------------------------------------------------
    //end Products
    //------------------------------------------------------------------------
    
    function addPrivateSaleEndDate(uint256 endDate) public onlyOwner {
        privateSaleEndDate = endDate;
    }
    
    function addPreSaleEndDate(uint256 endDate) public onlyOwner {
        preSaleEndDate = endDate;
    }
    
    function addICOEndDate(uint256 endDate) public onlyOwner {
        icoEndDate = endDate;
    }
    
    function addTeamAndAdvisoryMembers(address[] members) public onlyOwner 
    {
        uint totalTeamShare = uint(100000000).mul(10**uint(decimals));
        uint oneTeamMemberShare = totalTeamShare.div(members.length);
        for (uint i=0;i<members.length;i++)
        {
            teamMembers[members[i]] = oneTeamMemberShare;
            tokenBalances[owner] = tokenBalances[owner].sub(oneTeamMemberShare);
            tokenBalances[members[i]] = tokenBalances[members[i]].add(oneTeamMemberShare);
            emit Transfer(owner,members[i],oneTeamMemberShare);
        }    
    }
    function addPrivateSaleBuyer(address buyer,uint value) public onlyOwner 
    {
        privateSaleBuyerTokens[buyer] = value;        
    }
    function addPreSaleBuyer(address buyer,uint value) public onlyOwner 
    {
        preSaleBuyerTokens[buyer] = value;
    }
}
function balanceOf(address tokenOwner) public constant returns (uint balance) {
         return tokenBalances[tokenOwner];
     }
// Get the token balance for account `tokenOwner`