contract PreSell is Ownable,ERC721{
    using SafeMath for uint256;
    struct Coach{
        uint256 drawPrice;
        uint256 emoteRate;
        uint256 sellPrice;
        uint8   isSell;
        uint8   category;
    }
    event initialcoach(uint _id);
    event drawcoach(uint _id,address _owner);
    event purChase(uint _id, address _newowner, address _oldowner);
    event inviteCoachBack(address _from,address _to, uint _fee);
    Coach[] public originCoach;
    Coach[] public coaches; 
    mapping(uint=>address) coachToOwner;
    mapping(uint=>uint) public coachAllnums;
    mapping(address=>uint) ownerCoachCount;
    mapping (uint => address) coachApprovals;
    //modifier
    modifier onlyOwnerOf(uint _id) {
        require(msg.sender == coachToOwner[_id]);
        _;
    }
    //owner draw _money
    function withdraw() external onlyOwner {
        owner.transfer(address(this).balance);
    }
    //initial coach and coach nums;
    function initialCoach(uint _price,uint _emoterate,uint8 _category,uint _num) public onlyOwner{ 
      uint id = originCoach.push(Coach(_price,_emoterate,0,0,_category)) - 1;
      coachAllnums[id] = _num;
      emit initialcoach(id);
    }
    function drawCoach(uint _id,address _address) public payable{ 
        require(msg.value == originCoach[_id].drawPrice && coachAllnums[_id] > 0 );
        uint id = coaches.push(originCoach[_id]) -1;
        coachToOwner[id] = msg.sender;
        ownerCoachCount[msg.sender] = ownerCoachCount[msg.sender].add(1);
        coachAllnums[_id]  = coachAllnums[_id].sub(1);
        if(_address != 0){ 
                 uint inviteFee = msg.value * 5 / 100;
                 _address.transfer(inviteFee);
                 emit inviteCoachBack(msg.sender,_address,inviteFee);
        }
        emit drawcoach(_id,msg.sender);
    }
     //ERC721 functions;
    function balanceOf(address _owner) public view returns (uint256 _balance) {
        return ownerCoachCount[_owner];
    }

    function ownerOf(uint256 _tokenId) public view returns (address _owner) {
        return coachToOwner[_tokenId];
    }
    function _transfer(address _from, address _to, uint256 _tokenId) private {
        require(_to != _from);
        ownerCoachCount[_to] = ownerCoachCount[_to].add(1) ;
        ownerCoachCount[_from] = ownerCoachCount[_from].sub(1);
        coachToOwner[_tokenId] = _to;
        emit Transfer(_from, _to, _tokenId);
    }
    function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
        _transfer(msg.sender, _to, _tokenId);
    }

    function approve(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
        coachApprovals[_tokenId] = _to;
        emit Approval(msg.sender, _to, _tokenId);
    }
    function takeOwnership(uint256 _tokenId) public {
        require(coachApprovals[_tokenId] == msg.sender && coachToOwner[_tokenId] != msg.sender);
        address owner = ownerOf(_tokenId);
        _transfer(owner, msg.sender, _tokenId);
    }
    //market functions
        //market functions
    function setCoachPrice(uint _id,uint _price) public onlyOwnerOf(_id){ 
        coaches[_id].isSell = 1;
        coaches[_id].sellPrice = _price;
    }
    function coachTakeOff(uint _id) public onlyOwnerOf(_id){
        coaches[_id].isSell = 0;
    }
    function purchase(uint _id) public payable{
        require(coaches[_id].isSell == 1 && msg.value == coaches[_id].sellPrice && msg.sender != coachToOwner[_id]);
        address owner = coachToOwner[_id];
        ownerCoachCount[owner] = ownerCoachCount[owner].sub(1) ;
        ownerCoachCount[msg.sender] = ownerCoachCount[msg.sender].add(1);
        coachToOwner[_id] = msg.sender;
        owner.transfer(msg.value);
        emit purChase(_id,msg.sender,owner);
    }
    
}
function withdraw() external onlyOwner {
        owner.transfer(address(this).balance);
    }
//owner draw _money