contract SaleRecord {
    
    using SafeMath for uint256;

    struct sProperty {
        uint256 time;
        uint256 inputEther;
        uint256 outputToken;
        uint256 priceToken;
        bool withdraw;
    }
    
    sProperty propertyTotal;

    mapping (address => sProperty[]) propertyMember;
    address payable[] propertyKeys;
    
    function recordPropertyWithdraw(address _sender, uint256 _token) internal {
        for(uint256 i = 0; i < propertyMember[_sender].length; i++){
            if(propertyMember[_sender][i].withdraw == false && propertyMember[_sender][i].outputToken == _token){
                propertyMember[_sender][i].withdraw = true;
                break;
            }
        }
    }
    function recordProperty(address payable _sender, uint256 _amount, uint256 _token, uint256 _priceToken, bool _withdraw) internal {
        
        sProperty memory property = sProperty(now, _amount, _token, _priceToken, _withdraw);
        propertyMember[_sender].push(property);
        
        propertyTotal.time = now;
        propertyTotal.inputEther = propertyTotal.inputEther.add(_amount);
        propertyTotal.outputToken = propertyTotal.outputToken.add(_token);
        if (!contains(_sender)) {
            propertyKeys.push(_sender);
        }
    }
    function contains(address _addr) internal view returns (bool) {
        uint256 len = propertyKeys.length;
        for (uint256 i = 0 ; i < len; i++) {
            if (propertyKeys[i] == _addr) {
                return true;
            }
        }
        return false;
    }
    
    //get
    function getPropertyKeyCount() public view returns (uint){
        return propertyKeys.length;
    }
    function getPropertyInfo(address _addr) public view returns (uint256[] memory, uint256[] memory, uint256[] memory, uint256[] memory, bool[] memory){
        uint256[] memory time;
        uint256[] memory inputEther;
        uint256[] memory outputToken;
        uint256[] memory priceToken;
        bool[] memory withdraw;
        
        if(contains(_addr)){
            
            uint256 size = propertyMember[_addr].length;
            
            time = new uint256[](size);
            inputEther = new uint256[](size);
            outputToken = new uint256[](size);
            priceToken = new uint256[](size);
            withdraw = new bool[](size);
            
            for (uint i = 0 ; i < size ; i++) {
                time[i] = propertyMember[_addr][i].time;
                inputEther[i] = propertyMember[_addr][i].inputEther;
                outputToken[i] = propertyMember[_addr][i].outputToken;
                priceToken[i] = propertyMember[_addr][i].priceToken;
                withdraw[i] = propertyMember[_addr][i].withdraw;
            }
        } else {
            time = new uint256[](0);
            inputEther = new uint256[](0);
            outputToken = new uint256[](0);
            priceToken = new uint256[](0);
            withdraw = new bool[](0);
        }
        return (time, inputEther, outputToken, priceToken, withdraw);
        
    }
    function getPropertyValue(address _addr) public view returns (uint256, uint256){
        uint256 inputEther = 0;
        uint256 outputToken = 0;
        
        if(contains(_addr)){
            
            uint256 size = propertyMember[_addr].length;

            for (uint i = 0 ; i < size ; i++) {
                inputEther = inputEther.add(propertyMember[_addr][i].inputEther);
                outputToken = outputToken.add(propertyMember[_addr][i].outputToken);
            }
        } 
        
        return (inputEther, outputToken);
        
    }
    
    function getPropertyTotal() public view returns (uint256, uint256, uint256){
        return (propertyTotal.time, propertyTotal.inputEther, propertyTotal.outputToken);
    }
    
}
function getPropertyKeyCount() public view returns (uint){
    return propertyKeys.length;
}

//get