contract GreenClimateToken is Context, ERC20, ERC20Detailed,Ownable {
    
   
    struct consumerRecords{
        address wallet;
        uint256 Ti; //U4440M
        uint256 RH; //Relative humidity
        uint256 Td; 
        uint256 C; //Carbon
        uint256 P;
        uint256 Co2; 
        uint256 O2;
        uint256 BP; //Barometric Pressure
        uint256 timestamp;
        uint256 rewards;
        uint256 dateID;
    }
 
    uint256[] private consumers;
    
    mapping(uint256=>consumerRecords)private consumer;
    mapping(address=>bool)private isConsumer;
    mapping(uint256=>mapping(uint256=>consumerRecords))public recordsList;


    /**
     * @dev Constructor that gives _msgSender() all of existing tokens.
     */
    constructor () public ERC20Detailed("GreenClimateToken", "WGC", 18) {
        _mint(_msgSender(), 300000000 * (10 ** uint256(decimals())));
    }
    
    /**
     * @dev Function that registers an consumer in blockchain.
     */
    function registerConsumer(uint256 _consumerNumber,address _address) public onlyOwner returns(bool){
     
        consumers.push(_consumerNumber);
        isConsumer[_address] = true;
        consumer[_consumerNumber].wallet = _address;
        
        return true;
    }
    
     /**
     * @dev Function that stores an consumer records in blockchain.
     */
    function setConsumerReadings(uint256 _consumerNumber,uint256 _Ti,uint256 _RH,uint256 _Td,uint256 _C,uint256 _P,uint256 _Co2,uint256 _O2,uint256 _BP,uint256 _timestamp) public onlyOwner{
        //last updated value
        consumer[_consumerNumber].Ti = _Ti;
        consumer[_consumerNumber].RH = _RH;
        consumer[_consumerNumber].Td= _Td;
        consumer[_consumerNumber].C = _C;
        consumer[_consumerNumber].P = _P;
        consumer[_consumerNumber].Co2 = _Co2;
        consumer[_consumerNumber].O2 = _O2;
        consumer[_consumerNumber].BP = _BP;
        consumer[_consumerNumber].timestamp = _timestamp;
        consumer[_consumerNumber].dateID = consumer[_consumerNumber].dateID.add(1);
        
        //value by dateID
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Ti = _Ti;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].RH = _RH;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Td= _Td;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].C = _C;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].P = _P;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Co2 = _Co2;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].O2 = _O2;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].BP = _BP;
        recordsList[consumer[_consumerNumber].dateID][_consumerNumber].timestamp = _timestamp;
        
        
    }
    
     /**
     * @dev Returns the consumer data split1
     */
    function returnConsumerReadings1(uint256 _consumerNumber) public view returns(uint256,uint256,uint256,uint256,uint256) {
        return (consumer[_consumerNumber].Ti, consumer[_consumerNumber].RH, consumer[_consumerNumber].Td,
        consumer[_consumerNumber].C, consumer[_consumerNumber].P);
    }
    
    /**
     * @dev Returns the consumer data split2
     */
    function returnConsumerReadings2(uint256 _consumerNumber) public view returns(uint256,uint256,uint256,uint256) {
        return (consumer[_consumerNumber].Co2, consumer[_consumerNumber].O2, consumer[_consumerNumber].BP,consumer[_consumerNumber].timestamp);
    }
    
    
    /**
     * @dev Function that send reward tokens.
     */
    function sendRewards() public onlyOwner returns(bool){
        
        for(uint i=0;i<consumers.length;i++){
            if(consumer[consumers[i]].O2>=consumer[consumers[i]].Co2){
                transfer(consumer[consumers[i]].wallet,consumer[consumers[i]].O2.div(consumer[consumers[i]].Co2).mul(10).mul((10 ** uint256(decimals()))));
                consumer[consumers[i]].rewards = consumer[consumers[i]].rewards.add(consumer[consumers[i]].O2.div(consumer[consumers[i]].Co2).mul(10).mul((10 ** uint256(decimals()))));
            }
        }
    }
    
    function sendTimelyRewards(uint256[] memory _consumerNumber,uint256[] memory _Co2List,uint256[] memory _O2List)public onlyOwner{
         
        for(uint i=0;i<_consumerNumber.length;i++){
          transfer(consumer[_consumerNumber[i]].wallet, _O2List[i].div(_Co2List[i]).mul(10).mul((10 ** uint256(decimals()))));
           consumer[consumers[i]].rewards = consumer[consumers[i]].rewards.add(_O2List[i].div(_Co2List[i]).mul(10).mul((10 ** uint256(decimals()))));
        }
    }
    
     /**
     * @dev Returns the wallet address of a consumer 
     */
    function consumerWallet(uint256 _consumerNumber) public view returns(address){
        return consumer[_consumerNumber].wallet;
    }
    
     /**
     * @dev Returns the list of consumers
     */
    function consumerNumbers() public onlyOwner view returns(uint256[] memory){
        return consumers;
    }
    
     /**
     * @dev Returns the rewards earned by the consumer
     */
    function consumerRewards(uint256 _consumerNumber) public view returns(uint256){
        return (consumer[_consumerNumber].rewards);
    }
    
    
    
    
}
/**
 * @title GreenClimate Token
 * @dev Simple ERC20 Token , where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
function setConsumerReadings(uint256 _consumerNumber,uint256 _Ti,uint256 _RH,uint256 _Td,uint256 _C,uint256 _P,uint256 _Co2,uint256 _O2,uint256 _BP,uint256 _timestamp) public onlyOwner{
    //last updated value
    consumer[_consumerNumber].Ti = _Ti;
    consumer[_consumerNumber].RH = _RH;
    consumer[_consumerNumber].Td= _Td;
    consumer[_consumerNumber].C = _C;
    consumer[_consumerNumber].P = _P;
    consumer[_consumerNumber].Co2 = _Co2;
    consumer[_consumerNumber].O2 = _O2;
    consumer[_consumerNumber].BP = _BP;
    consumer[_consumerNumber].timestamp = _timestamp;
    consumer[_consumerNumber].dateID = consumer[_consumerNumber].dateID.add(1);
        
    //value by dateID
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Ti = _Ti;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].RH = _RH;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Td= _Td;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].C = _C;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].P = _P;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].Co2 = _Co2;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].O2 = _O2;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].BP = _BP;
    recordsList[consumer[_consumerNumber].dateID][_consumerNumber].timestamp = _timestamp;
        
        
}

/**
 * @dev Function that stores an consumer records in blockchain.
 */