/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.gradle.internal.model;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.annotations.concurrency.Immutable;
import com.android.builder.model.Library;
import com.android.builder.model.MavenCoordinates;
import com.google.common.base.Objects;

import java.io.Serializable;

/**
 * Serializable implementation of Library for use in the model.
 */
@Immutable
class LibraryImpl implements Library, Serializable {
    private static final long serialVersionUID = 1L;

    @Nullable
    private final String project;
    @Nullable
    private final String name;

    @Nullable
    private final MavenCoordinates requestedCoordinates;
    @NonNull
    private final MavenCoordinates resolvedCoordinates;

    private final boolean isSkipped;
    private final boolean isProvided;

    LibraryImpl(
            @Nullable String project,
            @Nullable MavenCoordinates requestedCoordinates,
            @NonNull MavenCoordinates resolvedCoordinates,
            boolean isSkipped,
            boolean isProvided) {
        this.name = resolvedCoordinates.toString();
        this.project = project;
        this.requestedCoordinates = requestedCoordinates;
        this.resolvedCoordinates = resolvedCoordinates;
        this.isSkipped = isSkipped;
        this.isProvided = isProvided;
    }

    protected LibraryImpl(@NonNull Library clonedLibrary) {
        name = clonedLibrary.getName();
        project = clonedLibrary.getProject();
        requestedCoordinates = clonedLibrary.getRequestedCoordinates();
        resolvedCoordinates = clonedLibrary.getResolvedCoordinates();
        isSkipped = clonedLibrary.isSkipped();
        isProvided = clonedLibrary.isProvided();
    }

    @Override
    @Nullable
    public String getProject() {
        return project;
    }

    @Nullable
    @Override
    public String getName() {
        return name;
    }

    @Nullable
    @Override
    public MavenCoordinates getRequestedCoordinates() {
        return requestedCoordinates;
    }

    @NonNull
    @Override
    public MavenCoordinates getResolvedCoordinates() {
        return resolvedCoordinates;
    }

    @Override
    public boolean isSkipped() {
        return isSkipped;
    }

    @Override
    public boolean isProvided() {
        return isProvided;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        LibraryImpl library = (LibraryImpl) o;
        return isSkipped == library.isSkipped &&
                isProvided == library.isProvided &&
                Objects.equal(project, library.project) &&
                Objects.equal(name, library.name) &&
                Objects.equal(requestedCoordinates, library.requestedCoordinates)
                &&
                Objects.equal(resolvedCoordinates, library.resolvedCoordinates);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(project, name, requestedCoordinates, resolvedCoordinates, isSkipped,
                isProvided);
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("name", name)
                .add("requestedCoordinates", requestedCoordinates)
                .add("resolvedCoordinates", resolvedCoordinates)
                .add("isSkipped", isSkipped)
                .add("isProvided", isProvided)
                .toString();
    }
}
