/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.gatein.pc.embed;

import org.gatein.pc.api.Mode;
import org.gatein.pc.api.Portlet;
import org.gatein.pc.api.PortletInvoker;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.WindowState;

import jakarta.servlet.ServletException;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**/
class Page
{

   /** . */
   final Map<String, String[]> parameters;

   /** . */
   final LinkedHashMap<String, Window> windows;

   Page(PortletInvoker invoker, String s) throws ServletException
   {
      LinkedHashMap<String, Window> windows = new LinkedHashMap<String, Window>();
      int count = 0;
      Map<String, String[]> parameters;

      //
      if (s == null ||  s.length() == 0 || (s.length() == 1 && s.charAt(0) == '/'))
      {
         parameters = new HashMap<String, String[]>();
      }
      else
      {
         Segment segments = (Segment)Chunk.parse(s);

         // Skip context path
         segments = (Segment)segments.next;

         // Skip /embed
         Chunk first = segments.next;

         //
         if (first instanceof Segment) {

            //
            parameters = segments.parameters != null ? segments.parameters : new HashMap<String, String[]>();
            segments = (Segment)first;

            //
            for (Segment segment : segments)
            {
               Portlet found = null;
               if (invoker != null)
               {
                  try
                  {
                     for (Portlet portlet : invoker.getPortlets())
                     {
                        if (portlet.getInfo().getName().equals(segment.value))
                        {
                           found = portlet;
                           break;
                        }
                     }
                  }
                  catch (PortletInvokerException e)
                  {
                     // ?
                  }
               }

               //
               LinkedHashMap<String, String[]> windowParameters;
               Mode windowMode;
               WindowState windowState;
               if (segment.parameters != null)
               {
                  windowParameters = new LinkedHashMap<String, String[]>(segment.parameters);
                  String[] modeParameter = windowParameters.remove("javax.portlet.portlet_mode");
                  String[] windowStateParameter = windowParameters.remove("javax.portlet.window_state");
                  windowMode = modeParameter != null ? Mode.create(modeParameter[0]) : null;
                  windowState = windowStateParameter != null ? WindowState.create(windowStateParameter[0]) : null;
               }
               else
               {
                  windowParameters = null;
                  windowMode = null;
                  windowState = null;
               }

               //
               Window context = new Window("" + count++, found, segment.value, windowMode, windowState, windowParameters);

               //
               windows.put(context.id, context);
            }
         } else {

            // Empty path case
            parameters = Collections.emptyMap();
         }
      }

      //
      this.windows = windows;
      this.parameters = parameters;
   }

   /**
    * Returns the windows that can consume the wanted event name.
    *
    * @param wanted the wanted event name
    * @return the collection of windows that can consume the wanted event
    */
   Collection<Window> getConsumers(QName wanted)
   {
      List<Window> consumers = null;
      for (Window window : windows.values())
      {
         if (window.portlet != null)
         {
            for (QName name : window.portlet.getInfo().getEventing().getConsumedEvents().keySet())
            {
               if (wanted.equals(name))
               {
                  if (consumers == null)
                  {
                     consumers = new ArrayList<Window>();
                  }
                  consumers.add(window);
               }
            }
         }
      }
      return consumers == null ? Collections.<Window>emptyList() : consumers;
   }
}
