contract BackToLife {

    using strings for *;

    address club;

    mapping (address => string) mapOwnerStringContract;

    /* Create base contract */
    function BackToLife () {
        club = msg.sender;
    }

    /* Create Last Will Contract */
    function createLastWill (address _owner, string _listHeirs, string _listHeirsPercentages, string _listWitnesses, uint256 _gasPrice, uint256 _gasCost) {

        address owner = _owner;

        var s = _listHeirs.toSlice().copy();

        if (!s.endsWith(";".toSlice())){
            _listHeirs.toSlice().concat(";".toSlice());
        }

        s = _listWitnesses.toSlice().copy();
        if (!s.endsWith(";".toSlice())){
            _listWitnesses.toSlice().concat(";".toSlice());
        }

        s = _listHeirsPercentages.toSlice().copy();
        if (!s.endsWith(";".toSlice())){
            _listHeirsPercentages.toSlice().concat(";".toSlice());
        }


        /* Add contract to the list of each heirs */
//        s = _listHeirs.toSlice().copy();
//        var delim = ";".toSlice();
//        uint256 listHeirsLength = s.count(delim) + 1;
//        string memory senderStringAddress = addressToString(owner);
//        for(uint i = 0; i < listHeirsLength; i++) {
//            address heirAddress = parseAddr(s.split(delim).toString());
//            mapOwnerStringContract[heirAddress] =  mapOwnerStringContract[heirAddress].toSlice().concat(stringContractAddress.toSlice()).toSlice().concat(";".toSlice());
//        }

        /* Calculate number of witness */
        s = _listWitnesses.toSlice().copy();
        var delim = ";".toSlice();
        uint256 listWitnessLength = s.count(delim) + 1;

        /* Create the My Will contract */
        address myWillAddress = new MyWill();
        MyWill myWillContract = MyWill(myWillAddress);
        myWillContract.setParameters(owner, _listHeirs, _listHeirsPercentages, _listWitnesses, club, _gasPrice, _gasCost);
        var myWillAddressString = addressToString(myWillAddress);
        mapOwnerStringContract[owner] =  mapOwnerStringContract[owner].toSlice().concat(myWillAddressString.toSlice()).toSlice().concat(";".toSlice());
    }

    /* Get Address Contracts */
    function getContracts(address owner) returns (string) {
        return mapOwnerStringContract[owner];
    }

    function addressToString(address x) returns (string) {
        bytes memory s = new bytes(42);
        s[0] = "0";
        s[1] = "x";
        for (uint i = 0; i < 20; i++) {
            byte b = byte(uint8(uint(x) / (2**(8*(19 - i)))));
            byte hi = byte(uint8(b) / 16);
            byte lo = byte(uint8(b) - 16 * uint8(hi));
            s[2+2*i] = char(hi);
            s[2+2*i+1] = char(lo);
        }
        return string(s);
    }

    function char(byte b) returns (byte c) {
        if (b < 10) return byte(uint8(b) + 0x30);
        else return byte(uint8(b) + 0x57);
    }

//    function parseAddr(string _a) internal returns (address){
//        bytes memory tmp = bytes(_a);
//        uint160 iaddr = 0;
//        uint160 b1;
//        uint160 b2;
//        for (uint i=2; i<2+2*20; i+=2){
//            iaddr *= 256;
//            b1 = uint160(tmp[i]);
//            b2 = uint160(tmp[i+1]);
//            if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87;
//            else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48;
//            if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87;
//            else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48;
//            iaddr += (b1*16+b2);
//        }
//        return address(iaddr);
//    }

    function getBalance() constant returns (uint) {
        return  address(this).balance;
    }

}
/* https://github.com/Arachnid/solidity-stringutils */
function BackToLife () {
    club = msg.sender;
}

/* Create base contract */