// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.browsers;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.PathUtil;
import com.intellij.xml.XmlBundle;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.swing.Icon;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

final class ConfigurableWebBrowser extends WebBrowser {
  private final UUID id;
  @NotNull private BrowserFamily family;
  @NotNull private String name;
  private boolean active;
  private String path;

  private BrowserSpecificSettings specificSettings;

  @SuppressWarnings("UnusedDeclaration")
  ConfigurableWebBrowser() {
    this(UUID.randomUUID(), BrowserFamily.CHROME);
  }

  ConfigurableWebBrowser(@NotNull UUID id, @NotNull BrowserFamily family) {
    this(id, family, family.getName(), family.getExecutionPath(), true, family.createBrowserSpecificSettings());
  }

  ConfigurableWebBrowser(@NotNull UUID id,
                                @NotNull BrowserFamily family,
                                @NotNull String name,
                                @Nullable String path,
                                boolean active,
                                @Nullable BrowserSpecificSettings specificSettings) {
    this.id = id;
    this.family = family;
    this.name = name;

    this.path = StringUtil.nullize(path);
    this.active = active;
    this.specificSettings = specificSettings;
  }

  public void setName(@NotNull String value) {
    name = value;
  }

  public void setFamily(@NotNull BrowserFamily value) {
    family = value;
  }

  @NotNull
  @Override
  public Icon getIcon() {
    if (family == BrowserFamily.CHROME) {
      if (WebBrowserManager.isYandexBrowser(this)) {
        return AllIcons.Xml.Browsers.Yandex;
      }
      else if (checkNameAndPath("Dartium") || checkNameAndPath("Chromium")) {
        return AllIcons.Xml.Browsers.Chromium;
      }
      else if (checkNameAndPath("Canary")) {
        return AllIcons.Xml.Browsers.Canary;
      }
      else if (checkNameAndPath("Opera")) {
        return AllIcons.Xml.Browsers.Opera;
      }
      else if (checkNameAndPath("node-webkit") || checkNameAndPath("nw") || checkNameAndPath("nwjs")) {
        return AllIcons.Xml.Browsers.Nwjs;
      }
    }
    else if (family == BrowserFamily.EXPLORER) {
      if (WebBrowserManager.isEdge(this)) {
        return AllIcons.Xml.Browsers.Edge;
      }
    }
    else if (family == BrowserFamily.FIREFOX) {
      if (checkNameAndPath("dev") || checkNameAndPath("Dev")) {
        return AllIcons.Xml.Browsers.FirefoxDeveloper;
      }
    }

    return family.getIcon();
  }

  private boolean checkNameAndPath(@NotNull String what) {
    return WebBrowserManager.checkNameAndPath(what, this);
  }

  @Nullable
  @Override
  public String getPath() {
    return path;
  }

  public void setPath(@Nullable String value) {
    path = PathUtil.toSystemIndependentName(StringUtil.nullize(value));
  }

  @Override
  @Nullable
  public BrowserSpecificSettings getSpecificSettings() {
    return specificSettings;
  }

  public void setSpecificSettings(@Nullable BrowserSpecificSettings value) {
    specificSettings = value;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean value) {
    active = value;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof ConfigurableWebBrowser)) {
      return false;
    }

    ConfigurableWebBrowser browser = (ConfigurableWebBrowser)o;
    return getId().equals(browser.getId()) &&
           family.equals(browser.family) &&
           active == browser.active &&
           Comparing.strEqual(name, browser.name) &&
           Objects.equals(path, browser.path) &&
           Comparing.equal(specificSettings, browser.specificSettings);
  }

  @Override
  public int hashCode() {
    return getId().hashCode();
  }

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

  @Override
  @NotNull
  public final UUID getId() {
    return id;
  }

  @Override
  @NotNull
  public BrowserFamily getFamily() {
    return family;
  }

  @Override
  @NotNull
  public String getBrowserNotFoundMessage() {
    return XmlBundle.message("error.0.browser.path.not.specified", getName());
  }

  @Override
  public void addOpenUrlParameter(@NotNull List<? super String> command, @NotNull String url) {
    if (WebBrowserManager.isEdge(this) && !command.isEmpty()) {
      command.set(command.size() - 1, command.get(command.size() - 1) + ":" + url);
    }
    else {
      super.addOpenUrlParameter(command, url);
    }
  }

  @Override
  public String toString() {
    return getName() + " (" + getPath() + ")";
  }
}
@@ -381,49 +381,86 @@
 ort 
-java.util.List;%0Aimport java.util.Objects;
+org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0A
 %0Aimp
@@ -471,18 +471,17 @@
 java
-.util.UUID
+x.swing.*
 ;%0Aim
@@ -493,105 +493,67 @@
 java
-x.swing.Icon;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
+.util.List;%0Aimport java.util.Objects;%0Aimport java.util.UUID
 ;%0A%0Af
@@ -2707,58 +2707,148 @@
     
-if (checkNameAndPath(%22dev%22) %7C%7C checkNameAndPath(%22D
+String path = getPath();%0A      if (StringUtil.containsIgnoreCase(getName(), %22dev%22) %7C%7C path != null && StringUtil.containsIgnoreCase(path, %22d
 ev%22)

check dir name too when detecting firefox dev (WEB-30930)