contract ScavengerHuntTokenWatch is MyAdvancedToken {
    uint public crowdsaleDeadline;
    uint public tokensDistributed;
    uint public totalHunters;
    uint public maxDailyRewards;
    string public scavengerHuntTokenName;
    string public scavengerHuntTokenSymbol;

    //Allow to stop being anonymous
    mapping (address => bytes32) public registeredNames;

    // 1 = Digged, No Reward
    // >1 X = Digged, Got Reward
    mapping (bytes32 => mapping (bytes32 => uint)) public GPSDigs;

    //Address of the person that got the reward
    mapping (bytes32 => mapping (bytes32 => address)) public GPSActivityAddress;

    //Maximize the daily reward
    mapping (address => mapping(uint => uint256) ) public dailyRewardCount;
    
    
    
    //Private
    uint256 digHashBase;
    bool crowdsaleClosed = false;

    event FundTransfer(address backer, uint amountEhter, uint amountScavengerHuntTokens, bool isContribution);
    event ShareLocation(address owner, uint ScavengerHuntTokenAmount, uint PercentageOfTotal, bytes32 GPSLatitude, bytes32 GPSLongitude);
    event ShareMessage(address recipient, string Message, string TokenName);
    event SaleEnded(address owner, uint totalTokensDistributed,uint totalHunters);
    event SharePersonalMessage(address Sender, string MyPersonalMessage, bytes32 GPSLatitude, bytes32 GPSLongitude);
    event NameClaimed(address owner, string Name, bytes32 GPSLatitude, bytes32 GPSLongitude);
    event HunterRewarded(address owner, uint ScavengerHuntTokenAmount, uint PercentageOfTotal, bytes32 GPSLatitude, bytes32 GPSLongitude);
    
    modifier afterDeadline() { if (now >= crowdsaleDeadline) _; }
    

    /**
     * Check if deadline was met, so close the sale of tokens
     */
    function checkDeadlinePassed() afterDeadline public {
        SaleEnded(owner, tokensDistributed,totalHunters);
        crowdsaleClosed = true;
    }

    
    /**
     * Constrctor function
     *
     * Setup the owner
     */
     function ScavengerHuntTokenWatch (
        address ifSuccessfulSendTo,
        uint durationInMinutes,
        uint weiCostOfEachToken,
        uint initialSupply,
        string tokenName,
        string tokenSymbol,
        uint256 adigHashBase,
        uint aMaxDailyRewards
        ) MyAdvancedToken(initialSupply, tokenName, tokenSymbol) public {
        owner=msg.sender;
        
        scavengerHuntTokenName = tokenName;
        scavengerHuntTokenSymbol = tokenSymbol;

        //Make sure we can get these tokens
        setPrices(0,weiCostOfEachToken * 1 wei);
       
        digHashBase = adigHashBase;
        maxDailyRewards = aMaxDailyRewards;

        crowdsaleDeadline = now + durationInMinutes * 1 minutes;
        tokensDistributed = initialSupply;
        FundTransfer(ifSuccessfulSendTo, 0, tokensDistributed, true);

        //Now change the owner to the actual user
        owner = ifSuccessfulSendTo;
        totalHunters=1;
        balanceOf[owner] = initialSupply;
        

    }


    function destroySHT(address _from, uint256 _value) internal {
        require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough
        balanceOf[_from] -= _value;                         // Subtract from the targeted balance
        totalSupply -= _value;                              // Update totalSupply
        if(balanceOf[_from]==0) {
            totalHunters--;
        }
    }

    

    function extendCrowdsalePeriod (uint durationInMinutes) onlyOwner public {
        crowdsaleDeadline = now + durationInMinutes * 1 minutes;
        crowdsaleClosed = false;
        ShareMessage(msg.sender,"The crowdsale is extended for token ->",scavengerHuntTokenName );
    }

    function setMaxDailyRewards(uint aMaxDailyRewards) onlyOwner public {
        maxDailyRewards = aMaxDailyRewards;
        ShareMessage(msg.sender,"The maximum of daily reward is now updated for token ->",scavengerHuntTokenName );
    }

    

    //We also allow the calling of the buying function too.
    function buyScavengerHuntToken() payable public {
        //Only allow buying in, when crowdsale is open.
        if (crowdsaleClosed) {
            ShareMessage(msg.sender,"Sorry: The crowdsale has ended. You cannot buy anymore ->",scavengerHuntTokenName );
        }
        require(!crowdsaleClosed);
        uint amountEth = msg.value;
        uint amountSht = amountEth / buyPrice;

        //Mint a token for each payer
        mintScavengerToken(msg.sender, amountSht);

        FundTransfer(msg.sender, amountEth, amountSht, true);
        
        
        //And check if fundraiser is closed:
        checkDeadlinePassed();
    }

    
    function buyScavengerHuntTokenWithLocationSharing(bytes32 GPSLatitude, bytes32 GPSLongitude) payable public {
        buyScavengerHuntToken();
        ShareLocation(msg.sender, balanceOf[msg.sender],getPercentageComplete(msg.sender), GPSLatitude, GPSLongitude);
    }

    
    /**
     * Fallback function
     *
     * The function without name is the default function that is called whenever anyone sends funds to a contract
     */
    function () payable public {
        buyScavengerHuntToken();
    }

    
    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintScavengerToken(address target, uint256 mintedAmount) private {
        if(balanceOf[target]==0) {
            //New hunter!
            totalHunters++;
        }else {}
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        Transfer(this, target, mintedAmount);
        tokensDistributed += mintedAmount;
    }


    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintExtraScavengerHuntTokens(address target, uint256 mintedAmount) onlyOwner public {
        mintScavengerToken(target, mintedAmount);
    }



    function shareScavengerHuntTokenLocation(bytes32 GPSLatitude, bytes32 GPSLongitude) public {
        //Only call this if you actually have tokens!
        require(balanceOf[msg.sender] > 0); 
        ShareLocation(msg.sender, balanceOf[msg.sender],getPercentageComplete(msg.sender), GPSLatitude, GPSLongitude);
    }

    function sharePersonalScavengerHuntTokenMessage(string MyPersonalMessage, bytes32 GPSLatitude, bytes32 GPSLongitude) public {
        //Only call this if you actually have tokens!
        require(balanceOf[msg.sender] >=1); 
        SharePersonalMessage(msg.sender, MyPersonalMessage, GPSLatitude, GPSLongitude);
        //Personal messages cost 1 token!
        destroySHT(msg.sender, 1);
    }

    function claimName(string MyName, bytes32 GPSLatitude, bytes32 GPSLongitude) public {
        //Only call this if you actually have tokens!
        require(bytes(MyName).length < 32);
        require(balanceOf[msg.sender] >= 10); 
        registeredNames[msg.sender]=getStringAsKey(MyName);
        NameClaimed(msg.sender, MyName, GPSLatitude, GPSLongitude);
        //Claiming your name costs 10 tokens!
        destroySHT(msg.sender, 10);
    }

    
    function transferScavengerHuntToken(address to, uint SHTokenAmount,bytes32 GPSLatitude, bytes32 GPSLongitude) public {
        //Share the transfer with the new total
        if(balanceOf[to]==0) {
            totalHunters++;
        }

        //Call the internal transfer method
        _transfer(msg.sender, to, SHTokenAmount);

        ShareLocation(to, balanceOf[to], getPercentageComplete(to), "unknown", "unknown");
        ShareLocation(msg.sender, balanceOf[msg.sender], getPercentageComplete(msg.sender), GPSLatitude, GPSLongitude);
        if(balanceOf[msg.sender]==0) {
            totalHunters--;
        }

    }

    function returnEtherumToOwner(uint amount) onlyOwner public {
        if (owner.send(amount)) {
            FundTransfer(owner, amount,0, false);
        } 
    }

    //Date and time library

    uint constant DAY_IN_SECONDS = 86400;
    uint constant YEAR_IN_SECONDS = 31536000;
    uint constant LEAP_YEAR_IN_SECONDS = 31622400;

    uint constant HOUR_IN_SECONDS = 3600;
    uint constant MINUTE_IN_SECONDS = 60;

    uint16 constant ORIGIN_YEAR = 1970;
    
    function leapYearsBefore(uint year) internal pure returns (uint) {
        year -= 1;
        return year / 4 - year / 100 + year / 400;
    }    
    
    function isLeapYear(uint16 year) internal pure returns (bool) {
                if (year % 4 != 0) {
                        return false;
                }
                if (year % 100 != 0) {
                        return true;
                }
                if (year % 400 != 0) {
                        return false;
                }
                return true;
    }
    
    function getDaysInMonth(uint8 month, uint16 year) internal pure returns (uint8) {
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                        return 31;
                }
                else if (month == 4 || month == 6 || month == 9 || month == 11) {
                        return 30;
                }
                else if (isLeapYear(year)) {
                        return 29;
                }
                else {
                        return 28;
                }
    }

    
    function getToday() public view returns (uint) {
        uint16 year;
        uint8 month;
        uint8 day;

        uint secondsAccountedFor = 0;
        uint buf;
        uint8 i;

        
        uint timestamp=now;
        
        // Year
        year = getYear(timestamp);
        buf = leapYearsBefore(year) - leapYearsBefore(ORIGIN_YEAR);

        secondsAccountedFor += LEAP_YEAR_IN_SECONDS * buf;
        secondsAccountedFor += YEAR_IN_SECONDS * (year - ORIGIN_YEAR - buf);

        // Month
        uint secondsInMonth;
        for (i = 1; i <= 12; i++) {
            secondsInMonth = DAY_IN_SECONDS * getDaysInMonth(i, year);
            if (secondsInMonth + secondsAccountedFor > timestamp) {
                    month = i;
                    break;
            }
            secondsAccountedFor += secondsInMonth;
        }

        // Day
        for (i = 1; i <= getDaysInMonth(month, year); i++) {
                if (DAY_IN_SECONDS + secondsAccountedFor > timestamp) {
                        day = i;
                        break;
                }
                secondsAccountedFor += DAY_IN_SECONDS;
        }
        
        //20170106
        uint endDate = uint(year) * 10000;
        if (month<10) {
            endDate += uint(month)*100;
        } else {
            endDate += uint(month)*10;
        }
        endDate += uint(day);
        return endDate;
        
    }

    function getYear(uint timestamp) internal pure returns (uint16) {
            uint secondsAccountedFor = 0;
            uint16 year;
            uint numLeapYears;

            // Year
            year = uint16(ORIGIN_YEAR + timestamp / YEAR_IN_SECONDS);
            numLeapYears = leapYearsBefore(year) - leapYearsBefore(ORIGIN_YEAR);

            secondsAccountedFor += LEAP_YEAR_IN_SECONDS * numLeapYears;
            secondsAccountedFor += YEAR_IN_SECONDS * (year - ORIGIN_YEAR - numLeapYears);

            while (secondsAccountedFor > timestamp) {
                    if (isLeapYear(uint16(year - 1))) {
                            secondsAccountedFor -= LEAP_YEAR_IN_SECONDS;
                    }
                    else {
                            secondsAccountedFor -= YEAR_IN_SECONDS;
                    }
                    year -= 1;
            }
            return year;
    }

    
    function hashSeriesNumber(bytes32 series, uint256 number) internal pure returns (bytes32){
        return keccak256(number, series);
    }
    
    function digRewardCheck(uint hash, uint modulo,uint reward,bytes32 GPSLatitude, bytes32 GPSLongitude) internal returns (uint256) {
        if (hash % modulo == 0) {
            //Reward a 50 tokens
            mintScavengerToken(msg.sender, reward);
            dailyRewardCount[msg.sender][getToday()]++;
            GPSDigs[GPSLatitude][GPSLongitude]=reward;
            GPSActivityAddress[GPSLatitude][GPSLongitude]=msg.sender;
            HunterRewarded(msg.sender, reward,getPercentageComplete(msg.sender), GPSLatitude, GPSLongitude);
            return reward;
        }
        else {
            return 0;
        }
    }

    function digForTokens(bytes32 GPSLatitude, bytes32 GPSLongitude) payable public returns(uint256) {
        //Only call this if you actually have tokens!
        require(balanceOf[msg.sender] > 1); 
        //Only once digging is allowed!
        require(GPSDigs[GPSLatitude][GPSLongitude] == 0); 

        //You can only win that much per day
        require( dailyRewardCount[msg.sender][getToday()] <= maxDailyRewards);
        
        //Diggin costs 1 tokens!
        destroySHT(msg.sender, 1);

        uint hash = uint(hashSeriesNumber(GPSLatitude,digHashBase));
        hash += uint(hashSeriesNumber(GPSLongitude,digHashBase));

        uint awarded = digRewardCheck(hash, 100000000,100000,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }

        awarded = digRewardCheck(hash, 100000,1000,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }
        
        awarded = digRewardCheck(hash, 10000,500,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }

        awarded = digRewardCheck(hash, 1000,200,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }

        awarded = digRewardCheck(hash, 100,50,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }

        awarded = digRewardCheck(hash, 10,3,GPSLatitude,GPSLongitude);
        if (awarded>0) {
            return awarded;
        }
        
        //You've got nothing!
        GPSDigs[GPSLatitude][GPSLongitude]=1;
        GPSActivityAddress[GPSLatitude][GPSLongitude]=msg.sender;
        HunterRewarded(msg.sender, 0,getPercentageComplete(msg.sender), GPSLatitude, GPSLongitude);
        return 0;
    }
    
    
    function getPercentageComplete(address ScavengerHuntTokenOwner)  view public returns (uint256){
        //Since there are no decimals, just create some of our own
        uint256 myBalance = balanceOf[ScavengerHuntTokenOwner]*100000.0;
        uint256 myTotalSupply = totalSupply;
        uint256 myResult = myBalance / myTotalSupply;
        return  myResult;
    }

    function getStringAsKey(string key) pure public returns (bytes32 ret) {
        require(bytes(key).length < 32);
        assembly {
          ret := mload(add(key, 32))
        }
    }
    
    function getKeyAsString(bytes32 x) pure public returns (string) {
        bytes memory bytesString = new bytes(32);
        uint charCount = 0;
        for (uint j = 0; j < 32; j++) {
            byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
            if (char != 0) {
                bytesString[charCount] = char;
                charCount++;
            }
        }
        bytes memory bytesStringTrimmed = new bytes(charCount);
        for (j = 0; j < charCount; j++) {
            bytesStringTrimmed[j] = bytesString[j];
        }
        return string(bytesStringTrimmed);
    }
    
    modifier aftercrowdsaleDeadline()  { if (now >= crowdsaleDeadline) _; }
    
}
function checkDeadlinePassed() afterDeadline public {
        SaleEnded(owner, tokensDistributed,totalHunters);
        crowdsaleClosed = true;
    }
/**
 * Check if deadline was met, so close the sale of tokens
 */