contract GFToken is ERC20, Ownable {
    string private _name;
    string private _symbol;
    uint8 private _decimals;
    
    mapping(address => bool) private _whiteList;
    uint256[] private _tradingOpenTime;
    mapping(address => bool) private _quitLock;
    mapping(bytes32 => bool) private _batchRecord;
    
    constructor(string name, string symbol, uint8 decimals, uint256 _total) public {
        _name = name;
        _symbol = symbol;
        _decimals = decimals;
        
        _mint(msg.sender, _total.mul(10 ** uint256(_decimals)));
        _whiteList[msg.sender] = true;
    }
    
    // detail info
    function name() public view returns (string) {
        return _name;
    }
    
    function symbol() public view returns (string) {
        return _symbol;
    }
    
    function decimals() public view returns (uint8) {
        return _decimals;
    }
    
    // transfer ownership and balance
    function transferOwnership(address newOwner) public onlyOwner {
        _whiteList[newOwner] = true;
        super.transfer(newOwner, balanceOf(msg.sender));
        _whiteList[msg.sender] = false;
        super.transferOwnership(newOwner);
    }
    
    // whiteList
    function inWhiteList(address addr) public view returns (bool) {
        return _whiteList[addr];
    }
    
    function setWhiteList(address[] addressArr, bool[] statusArr) public onlyOwner {
        require(addressArr.length == statusArr.length, "The length of address array is not equal to the length of status array!");
        
        for(uint256 idx = 0; idx < addressArr.length; idx++) {
            _whiteList[addressArr[idx]] = statusArr[idx];
        }
    }
    
    // trading open time
    function setTradingTime(uint256[] times) public onlyOwner {
        require(times.length.mod(2) == 0, "the length of times must be even number");
        
        for(uint256 idx = 0; idx < times.length; idx = idx+2) {
            require(times[idx] < times[idx+1], "end time must be greater than start time");
        }
        _tradingOpenTime = times;
    }
    
    function getTradingTime() public view returns (uint256[]) {
        return _tradingOpenTime;
    }
    
    function inTradingTime() public view returns (bool) {
        for(uint256 idx = 0; idx < _tradingOpenTime.length; idx = idx+2) {
            if(now > _tradingOpenTime[idx] && now < _tradingOpenTime[idx+1]) {
                return true;
            }
        }
        return false;
    }
    
    // quit
    function inQuitLock(address account) public view returns (bool) {
        return _quitLock[account];
    }
    
    function setQuitLock(address account) public onlyOwner {
        require(inWhiteList(account), "account is not in whiteList");
        _quitLock[account] = true;
    }
    
    function removeQuitAccount(address account) public onlyOwner {
        require(inQuitLock(account), "the account is not in quit lock status");
        
        forceTransferBalance(account, _owner, balanceOf(account));
        _whiteList[account] = false;
        _quitLock[account] = false;
    }
    
    // overwrite transfer and transferFrom
    function transfer(address to, uint256 value) public returns (bool) {
        require(inWhiteList(msg.sender), "caller is not in whiteList");
        require(inWhiteList(to), "to address is not in whiteList");
        
        if(!inQuitLock(msg.sender) && !isOwner()) {
            require(inTradingTime(), "now is not trading time");
        }
        return super.transfer(to, value);
    }
    
    function transferFrom(address from, address to, uint256 value) public returns (bool) {
        require(inWhiteList(from), "from address is not in whiteList");
        require(inWhiteList(to), "to address is not in whiteList");
        
        if(!inQuitLock(msg.sender)) {
            require(inTradingTime(), "now is not trading time");
        }
        return super.transferFrom(from, to, value);
    }
    
    // force transfer balance
    function forceTransferBalance(address from, address to, uint256 value) public onlyOwner {
        require(inWhiteList(to), "to address is not in whiteList");
        _transfer(from, to, value);
    }
    
    // repalce account
    function replaceAccount(address oldAccount, address newAccount) public onlyOwner {
        require(inWhiteList(oldAccount), "old account is not in whiteList");
        _whiteList[newAccount] = true;
        forceTransferBalance(oldAccount, newAccount, balanceOf(oldAccount));
        _whiteList[oldAccount] = false;
    }
    
    // batch transfer
    function batchTransfer(bytes32 batch, address[] addressArr, uint256[] valueArr) public onlyOwner {
        require(addressArr.length == valueArr.length, "The length of address array is not equal to the length of value array!");
        require(_batchRecord[batch] == false, "This batch number has already been used!");
        
        for(uint256 idx = 0; idx < addressArr.length; idx++) {
            require(transfer(addressArr[idx], valueArr[idx]));
        }
        
        _batchRecord[batch] = true;
    }
    
    // mint and burn
    function mint(address account, uint256 value) public onlyOwner returns (bool) {
        require(inWhiteList(account), "account is not in whiteList");
        _mint(account, value);
    }
    
    function burn(address account, uint256 value) public onlyOwner returns (bool) {
        _burn(account, value);
        return true;
    }
}
function batchTransfer(bytes32 batch, address[] addressArr, uint256[] valueArr) public onlyOwner {
        require(addressArr.length == valueArr.length, "The length of address array is not equal to the length of value array!");
        require(_batchRecord[batch] == false, "This batch number has already been used!");
        
        for(uint256 idx = 0; idx < addressArr.length; idx++) {
            require(transfer(addressArr[idx], valueArr[idx]));
        }
        
        _batchRecord[batch] = true;
    }
// batch transfer