/*
 * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.xbib.net.mail.imap.protocol;

import jakarta.mail.Flags;
import org.xbib.net.mail.iap.ParsingException;

/**
 * This class
 *
 * @author John Mani
 */

public class FLAGS extends Flags implements Item {

    // IMAP item name
    static final char[] name = {'F', 'L', 'A', 'G', 'S'};
    public int msgno;

    /**
     * Constructor.
     *
     * @throws ParsingException for parsing failures
     * @param    r    the IMAPResponse
     */
    @SuppressWarnings("this-escape")
    public FLAGS(IMAPResponse r) throws ParsingException {
        msgno = r.getNumber();

        r.skipSpaces();
        String[] flags = r.readSimpleList();
        if (flags != null) { // if not empty flaglist
            for (int i = 0; i < flags.length; i++) {
                String s = flags[i];
                if (s.length() >= 2 && s.charAt(0) == '\\') {
                    switch (Character.toUpperCase(s.charAt(1))) {
                        case 'S': // \Seen
                            add(Flag.SEEN);
                            break;
                        case 'R': // \Recent
                            add(Flag.RECENT);
                            break;
                        case 'D':
                            if (s.length() >= 3) {
                                char c = s.charAt(2);
                                if (c == 'e' || c == 'E') // \Deleted
                                    add(Flag.DELETED);
                                else if (c == 'r' || c == 'R') // \Draft
                                    add(Flag.DRAFT);
                            } else
                                add(s);    // unknown, treat it as a user flag
                            break;
                        case 'A': // \Answered
                            add(Flag.ANSWERED);
                            break;
                        case 'F': // \Flagged
                            add(Flag.FLAGGED);
                            break;
                        case '*': // \*
                            add(Flag.USER);
                            break;
                        default:
                            add(s);        // unknown, treat it as a user flag
                            break;
                    }
                } else
                    add(s);
            }
        }
    }
}
