import jenkins.model.Jenkins
import org.codefirst.SimpleThemeDecorator
import org.jenkinsci.plugins.simpletheme.CssTextThemeElement

def instance = Jenkins.get()
def decorators = instance.getExtensionList(SimpleThemeDecorator.class)

if (decorators?.size() > 0) {
  def theme = decorators[0]
  def cssRules = """
:root,
.app-theme-picker__picker[data-theme=none] {
  --background: #0f1216 !important;
  --header-background: #141922 !important;
  --header-border: #2b313b !important;
  --white: #141922 !important;
  --black: #e6e9ef !important;
  --very-light-grey: #171b21 !important;
  --light-grey: #202734 !important;
  --medium-grey: #2b313b !important;
  --dark-grey: #0b0f14 !important;
  --text-color: #e6e9ef !important;
  --text-color-secondary: #a6adba !important;
  --card-background: #171b21 !important;
  --card-border-color: #2b313b !important;
  --pane-header-bg: #1f252d !important;
  --pane-header-border-color: #2b313b !important;
  --pane-border-color: #2b313b !important;
  --pane-text-color: #e6e9ef !important;
  --pane-header-text-color: #e6e9ef !important;
  --link-color: #8fb7ff !important;
  --link-color--hover: #b0ccff !important;
  --link-dark-color: #e6e9ef !important;
  --link-dark-color--hover: #b0ccff !important;
  --input-color: #151a20 !important;
  --input-border: #2b313b !important;
  --input-border-hover: #3a424d !important;
  --button-background: #232a33 !important;
  --button-background--hover: #2b313b !important;
  --button-background--active: #323b46 !important;
  --item-background--hover: #232a33 !important;
  --item-background--active: #2b313b !important;
  --accent-color: #8fb7ff !important;
}

body,
#page-body,
#page-header,
#header,
#main-panel,
#main-panel-content,
#side-panel,
.top-sticker-inner,
.bottom-sticker-inner,
#breadcrumbBar,
#breadcrumbs {
  background-color: var(--background) !important;
  color: var(--text-color) !important;
}

.jenkins-card,
.jenkins-section,
.jenkins-section__item,
#main-panel .jenkins-card,
#main-panel .jenkins-section {
  background-color: var(--card-background) !important;
  color: var(--text-color) !important;
  border-color: var(--card-border-color) !important;
}

table.pane,
table.pane td,
table.pane th,
#projectstatus td,
#projectstatus th {
  background-color: var(--card-background) !important;
  color: var(--text-color) !important;
}

table.pane tr:nth-child(even) td,
#projectstatus tr:hover td {
  background-color: #1f252d !important;
}

input,
select,
textarea,
#search-box {
  background-color: #151a20 !important;
  color: var(--text-color) !important;
  border-color: var(--input-border) !important;
}

a,
a:visited,
a:link {
  color: var(--link-color) !important;
}

a:hover {
  opacity: 0.85;
}

#side-panel .task-link,
#breadcrumbs a,
#breadcrumbs,
#projectstatus th a {
  color: var(--text-color-secondary) !important;
}

.console-output,
.console-output pre,
pre,
code,
.CodeMirror {
  background-color: #0c0f14 !important;
  color: #d9dee7 !important;
}

#footer {
  background-color: var(--background) !important;
  color: var(--text-color-secondary) !important;
}

.jenkins_ver:after {
  content: "atlas dark";
}
""".stripIndent().trim()

  theme.setElements([new CssTextThemeElement(cssRules)])
  theme.setCssUrl("")
  theme.setCssRules(cssRules)
  theme.setJsUrl("")
  theme.save()
  println("Applied simple-theme-plugin dark theme")
} else {
  println("simple-theme-plugin not installed; skipping theme configuration")
}
