contract BearCoin is BurnableToken, MintableToken, LimitedTransferToken, Pausable, HasNoEther {
	struct Tether {
		bytes5 currency;
		uint32 amount;
		uint32 price;
		uint32 startBlock;
		uint32 endBlock;
	}

	address[] public addressById;
	mapping (string => uint256) idByName;
	mapping (address => string) nameByAddress;

	// Ether/Wei have the same conversion as Bear/Cub
	uint256 public constant INITIAL_SUPPLY = 2000000 ether;

	string public constant symbol = "BEAR";
	uint256 public constant decimals = 18;
	string public constant name = "BearCoin";

	string constant genesis = "CR30001";
	uint256 public genesisBlock = 0;

	mapping (address => Tether[]) public currentTethers;
	address public controller;

	event Tethered(address indexed holder, string holderName, string currency, uint256 amount, uint32 price, uint256 indexed tetherID, uint timestamp, string message);
	event Untethered(address indexed holder,string holderName, string currency, uint256 amount, uint32 price, uint32 finalPrice, uint256 outcome, uint256 indexed tetherID, uint timestamp);
	event NameRegistered(address indexed a, uint256 id, string userName, uint timestamp);
	event NameChanged(address indexed a, uint256 id, string newUserName, string oldUserName, uint timestamp);

	function BearCoin() {
		balances[msg.sender] = INITIAL_SUPPLY;
		totalSupply = INITIAL_SUPPLY;
		addressById.push(0x0);
		idByName[genesis] = 0;
		nameByAddress[0x0] = genesis;
		genesisBlock = block.number;
	}

	// Non-upgradable function required for LimitedTransferToken
	function transferableTokens(address holder, uint64 time) constant public returns (uint256) {
		uint256 count = tetherCount(holder);

		if (count == 0) return super.transferableTokens(holder, time);

		uint256 tetheredTokens = 0;
		for (uint256 i = 0; i < count; i++) {
			// All tethers are initialized with an endBlock of 0
			if (currentTethers[holder][i].endBlock == 0) {
				tetheredTokens = tetheredTokens.add(_finneyToWei(currentTethers[holder][i].amount));
			}
		}

		return balances[holder].sub(tetheredTokens);
	}

	// only x modifiers
	modifier onlyController() {
		require(msg.sender == controller);
		_;
	}

	// Set roles
	function setController(address a) onlyOwner {
		controller = a;
	}

	// Controller-only functions
	function addTether(address a, string _currency, uint256 amount, uint32 price, string m) external onlyController whenNotPaused {
		// Make sure there are enough BearCoins to tether
		require(transferableTokens(a, 0) >= amount);
		bytes5 currency = _stringToBytes5(_currency);
		uint256 count = currentTethers[a].push(Tether(currency, _weiToFinney(amount), price, uint32(block.number.sub(genesisBlock)), 0));
		Tethered(a, nameByAddress[a], _currency, amount, price, count - 1, now, m);
	}
	function setTether(address a, uint256 tetherID, uint32 finalPrice, uint256 outcome) external onlyController whenNotPaused {
		currentTethers[a][tetherID].endBlock = uint32(block.number.sub(genesisBlock));
		Tether memory tether = currentTethers[a][tetherID];
		Untethered(a, nameByAddress[a], _bytes5ToString(tether.currency), tether.amount, tether.price, finalPrice, outcome, tetherID, now);
	}
	function controlledMint(address _to, uint256 _amount) external onlyController whenNotPaused returns (bool) {
		totalSupply = totalSupply.add(_amount);
		balances[_to] = balances[_to].add(_amount);
		Mint(_to, _amount);
		Transfer(0x0, _to, _amount);
		return true;
	}
	function controlledBurn(address _from, uint256 _value) external onlyController whenNotPaused returns (bool) {
		require(_value > 0);

		balances[_from] = balances[_from].sub(_value);
		totalSupply = totalSupply.sub(_value);
		Burn(_from, _value);
		return true;
	}

	function registerName(address a, string n) external onlyController whenNotPaused {
		require(!isRegistered(a));
		require(getIdByName(n) == 0);
		require(a != 0x0);
		require(_nameValid(n));
		uint256 length = addressById.push(a);
		uint256 id = length - 1;
		idByName[_toLower(n)] = id;
		nameByAddress[a] = n;
		NameRegistered(a, id, n, now);
	}
	function changeName(address a, string n) external onlyController whenNotPaused {
		require(isRegistered(a));
		require(getIdByName(n) == 0);
		require(a != 0x0);
		require(_nameValid(n));
		string memory old = nameByAddress[a];
		uint256 id = getIdByName(old);
		idByName[_toLower(old)] = 0;
		idByName[_toLower(n)] = id;
		nameByAddress[a] = n;
		NameChanged(a, id, n, old, now);
	}

	// Getters
	function getTether(address a, uint256 tetherID) public constant returns (string, uint256, uint32, uint256, uint256) {
		Tether storage tether = currentTethers[a][tetherID];
		return (_bytes5ToString(tether.currency), _finneyToWei(tether.amount), tether.price, uint256(tether.startBlock).add(genesisBlock), uint256(tether.endBlock).add(genesisBlock));
	}
	function getTetherInts(address a, uint256 tetherID) public constant returns (uint256, uint32, uint32, uint32) {
		Tether memory tether = currentTethers[a][tetherID];
		return (_finneyToWei(tether.amount), tether.price, tether.startBlock, tether.endBlock);
	}
	function tetherCount(address a) public constant returns (uint256) {
		return currentTethers[a].length;
	}
	function getAddressById(uint256 id) returns (address) {
		return addressById[id];
	}
	function getIdByName(string n) returns (uint256) {
		return idByName[_toLower(n)];
	}
	function getNameByAddress(address a) returns (string) {
		return nameByAddress[a];
	}
	function getAddressCount() returns (uint256) {
		return addressById.length;
	}

	// Utility functions
	function verifyTetherCurrency(address a, uint256 tetherID, string currency) public constant returns (bool) {
		return currentTethers[a][tetherID].currency == _stringToBytes5(currency);
	}
	function verifyTetherLoss(address a, uint256 tetherID, uint256 price) public constant returns (bool) {
		return currentTethers[a][tetherID].price < uint32(price);
	}
	function isRegistered(address a) returns (bool) {
		return keccak256(nameByAddress[a]) != keccak256('');
	}

	// Internal helper functions
	function _nameValid(string s) internal returns (bool) {
		return bytes(s).length != 0 && keccak256(s) != keccak256(genesis) && bytes(s).length <= 32;
	}
	function _bytes5ToString(bytes5 b) internal returns (string memory s) {
		bytes memory bs = new bytes(5);
		for (uint8 i = 0; i < 5; i++) {
			bs[i] = b[i];
		}
		s = string(bs);
	}
	function _stringToBytes5(string memory s) internal returns (bytes5 b) {
		assembly {
			b := mload(add(s, 32))
		}
	}
	function _toLower(string str) internal returns (string) {
		bytes memory bStr = bytes(str);
		bytes memory bLower = new bytes(bStr.length);
		for (uint i = 0; i < bStr.length; i++) {
			// Uppercase character...
			if ((bStr[i] >= 65) && (bStr[i] <= 90)) {
				// So we add 32 to make it lowercase
				bLower[i] = bytes1(int(bStr[i]) + 32);
			} else {
				bLower[i] = bStr[i];
			}
		}
		return string(bLower);
	}
	function _finneyToWei(uint32 _n) returns (uint256) {
		uint256 n = uint256(_n);
		uint256 f = 1 finney;
		return n.mul(f);
	}
	function _weiToFinney(uint256 n) returns (uint32) {
		uint256 f = 1 finney;
		uint256 p = n.div(f);
		return uint32(p);
	}

}
function setController(address a) onlyOwner {
	controller = a;
}

// Set roles