/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.data.manipulator.immutable.entity;

import com.google.common.collect.ComparisonChain;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.entity.ImmutableJoinData;
import org.spongepowered.api.data.manipulator.mutable.entity.JoinData;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.common.data.manipulator.immutable.common.AbstractImmutableData;
import org.spongepowered.common.data.manipulator.mutable.entity.SpongeJoinData;
import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;

import java.time.Instant;

public class ImmutableSpongeJoinData extends AbstractImmutableData<ImmutableJoinData, JoinData> implements ImmutableJoinData {

    private final ImmutableSpongeValue<Instant> firstJoined;
    private final ImmutableSpongeValue<Instant> lastJoined;

    public ImmutableSpongeJoinData(Instant firstJoined, Instant lastJoined) {
        super(ImmutableJoinData.class);
        this.firstJoined = new ImmutableSpongeValue<>(Keys.FIRST_DATE_PLAYED, Instant.EPOCH, firstJoined);
        this.lastJoined = new ImmutableSpongeValue<>(Keys.LAST_DATE_PLAYED, Instant.EPOCH, lastJoined);
        registerGetters();
    }

    @Override
    protected void registerGetters() {
        registerFieldGetter(Keys.FIRST_DATE_PLAYED, this.firstJoined::get);
        registerKeyValue(Keys.FIRST_DATE_PLAYED, this::firstPlayed);

        registerFieldGetter(Keys.LAST_DATE_PLAYED, this.lastJoined::get);
        registerKeyValue(Keys.LAST_DATE_PLAYED, this::lastJoined);
    }

    @Override
    public ImmutableValue<Instant> firstPlayed() {
        return this.firstJoined;
    }

    @Override
    public ImmutableValue<Instant> lastJoined() {
        return this.lastJoined;
    }

    @Override
    public JoinData asMutable() {
        return new SpongeJoinData(this.firstJoined.get(), this.lastJoined.get());
    }

    @Override
    public int compareTo(ImmutableJoinData o) {
        return ComparisonChain.start()
                .compare(o.firstPlayed().get(), this.firstJoined.get())
                .compare(o.lastJoined().get(), this.lastJoined.get())
                .result();
    }

    @Override
    public DataContainer toContainer() {
        return super.toContainer()
                .set(Keys.FIRST_DATE_PLAYED.getQuery(), this.firstJoined.get().toEpochMilli())
                .set(Keys.LAST_DATE_PLAYED.getQuery(), this.lastJoined.get().toEpochMilli());
    }
}

@@ -2884,28 +2884,28 @@
 , this::last
-Join
+Play
 ed);%0A    %7D%0A%0A
@@ -3058,28 +3058,28 @@
 nstant%3E last
-Join
+Play
 ed() %7B%0A     
@@ -3453,20 +3453,20 @@
 e(o.last
-Join
+Play
 ed().get

Update to lastJoined -> lastPlayed rename