import m from 'mithril'
import {NavHelper} from './helpers/nav.ts'
import htmlPage from './common/htmlPage.ts'

export default async (
  reposConfig: any,
  eleventyConfig: any,
  data: any,
) => {
  const pagesJustForBranch = eleventyConfig.getFilter("pagesJustForBranch")
  const slugify = eleventyConfig.getFilter("slugify")
  const date = eleventyConfig.getFilter("date")

  const nav = NavHelper(reposConfig, slugify, data.patchPage.repoName, data.patchPage.branchName)

  const pageContent = [
    m('div', {class: "row mt-3 mb-1"},
      m('div', {class: "col"},
        m('p', {class: "d-inline-block"}, [
          'Showing commits from ',
          m('span', {class: "font-monospace me-2"}, data.patchPage.branchName),
          m('a', {
            class: "btn btn-outline-primary badge shadow-none",
            href: nav.repoCurrentBranchRssFeed()
          }, [
            m('img', {
              src: `${nav.rootPath()}frontend/img/rss-icon.svg`,
              style: "width: 11px; margin-right: 5px;"
            }),
            'RSS',
          ])
        ])
      )
    ),
    m('nav', 
      m('ul', {class: "pagination"},
        pagesJustForBranch(
          data.paginatedPatches,
          data.patchPage.repoName,
          data.patchPage.branchName
        ).map((pageObj) => {
          return m('li', {class: "page-item"},
            m('a', {
              class: `page-link ${pageObj.pageNumber === data.patchPage.pageNumber ? 'active' : ''}`,
              href: `${data.reposPath}/${slugify(data.patchPage.repoName)}/branches/${data.patchPage.branchName}/commits/page${pageObj.pageNumber}`
            }, pageObj.pageNumber)
          )
        })
      )
    ),
    m('div', data.patchPage.commits.map((commit) => {
      return m('div', {
        class: "bezel-gray p-2 my-2",
        style: "max-width: 40rem;"
      }, [
        commit.isMerge ? m('span', {class: 'badge rounded-pill bg-primary me-1'}, 'merge') : null,
        m('a', {
          class: "fs-5",
          href: `${data.reposPath}/${slugify(data.patchPage.repoName)}/branches/${slugify(data.patchPage.branchName)}/commits/${commit.hash}`,
        }, commit.message.split('\n').slice(0, 1)),
        m('br'),
        m('span', date(commit.date)),
        m('br'),
        m('span', commit.author),
        m('p', {class: "commit-hash font-monospace mb-0"}, commit.hash),
      ])
    })),
    m('nav',
      m('ul', {class: "pagination"},
        pagesJustForBranch(
          data.paginatedPatches,
          data.patchPage.repoName,
          data.patchPage.branchName
        ).map((pageObj) => {
          return m('li', {class: "page-item"},
            m('a', {
              class: `page-link ${pageObj.pageNumber === data.patchPage.pageNumber ? 'active' : ''}`,
              href: `${data.reposPath}/${slugify(data.patchPage.repoName)}/branches/${data.patchPage.branchName}/commits/page${pageObj.pageNumber}`
            }, pageObj.pageNumber)
          )
        })
      )
    )
  ]

  return await htmlPage(reposConfig, eleventyConfig, data, pageContent)
}
