/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.ignite.jiraservice;

import com.google.common.base.MoreObjects;

import javax.annotation.Nullable;

/**
 * See example of GSON here https://issues.apache.org/jira/rest/api/2/issue/IGNITE-123
 */
public class Ticket {
    public static final String PROJECT_DELIM = "-";
    /** Id. */
    public long id;

    /** Ticket full name like "IGNITE-123". */
    public String key;

    /** Fields. */
    public Fields fields;

    public Ticket() {}

    public Ticket(String ticketKey) {
        this.key = ticketKey;
    }


    /**
     * @param projectCode JIRA project code to be removed from ticket key.
     * @return Ignite ticket Number ignoring project code (like 123 in IGNITE-123).
     */
    public int keyWithoutProject(String projectCode) {
        String ticketPrefix = projectCode + PROJECT_DELIM;

        return Integer.valueOf(key.substring(ticketPrefix.length()));
    }

    /**
     * @return Ticket status (open, resolved, etc);
     */
    @Nullable
    public String status() {
        if (fields == null || fields.status == null)
            return null;

        return fields.status.name;
    }

    /** {@inheritDoc} */
    @Override public String toString() {
        return MoreObjects.toStringHelper(this)
            .add("id", id)
            .add("key", key)
            .add("fields", fields)
            .toString();
    }

    /**
     * Checks if ticket relates to some Active (In progress/Patch Available) Contribution.
     */
    public boolean isActiveContribution() {
        String status = status();

        return Status.PA_NAME.equals(status)
            || Status.IP_NAME.equals(status)
            || Status.OPEN_NAME.equals(status)
            || Status.BACKLOG_NAME.equals(status)
            || Status.REOPENED_NAME.equals(status);
    }
}