import axios from 'axios'
import fs from 'fs'
import formData from 'form-data'

const env = process.env.NODE_ENV || 'testing'
const config = require('../config/config')[env]

const gitlabController = {
  getUserByEmail: async function (email: string) {
    return await axios({
      method: 'get',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users?search=' + email,
      headers: { Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects }
    })
      .then(res => res.data[0])
      .catch(function (err) {
        console.error(err)
        return null
      })
  },
  createNewPages: async function (newPagesData: any, newLogoFile: string, template: any) {
    const data = new formData()
    data.append('avatar', fs.createReadStream(newLogoFile))

    return await axios({
      method: 'post',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/user/' + String(newPagesData.getOwnerGitlabId()) +
                '?name=' + String(newPagesData.getName()) + '&description=' + String(newPagesData.getDesc()) + '&tag_list=website' +
                '&use_custom_template=true&template_name=' + template,
      headers: {
        Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects,
        ...data.getHeaders()
      },
      data: data
    })
      .then(res => res.data)
      .catch(function (err) {
        console.error('ERR Status: ' + err.response.status)
        console.error('ERR Name: ' + err.response.data.message.name)
        console.error('ERR Path: ' + err.response.data.message.path)
        return err.response
      })
  },
  updateProject: async function (updatedProjectData: any, newLogoFile: string) {
    const data = new formData()
    if (newLogoFile) {
      data.append('avatar', fs.createReadStream(newLogoFile))
    }

    return await axios({
      method: 'put',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/' + updatedProjectData.getId() +
                '?name=' + updatedProjectData.getName() + '&description=' + updatedProjectData.getDesc(),
      headers: {
        Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects,
        ...data.getHeaders()
      },
      data: data
    })
    // .then(res => res.data[0])
      .then(res => res.data)
      .catch(function (err) {
        console.error('ERR Status: ' + err.response.status)
        console.error('ERR Name: ' + err.response.data.message.name)
        console.error('ERR Path: ' + err.response.data.message.path)
        return err.response
      })
  },
  deleteProjectById: async function (projectId: number) {
    // https://docs.gitlab.com/ee/api/projects.html#delete-project
    return await axios({
      method: 'delete',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/' + projectId,
      headers: {
        Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects
      }
    })
      .then(res => true)
      .catch(function (err) {
        console.error('ERR Status: ' + err.response.status)
        console.error('ERR Name: ' + err.response.data.message.name)
        console.error('ERR Path: ' + err.response.data.message.path)
        return false
      })
  },
  getUserProjects: async function (gitlabUserId: number) {
    return await axios({
      method: 'get',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/users/' + gitlabUserId + '/projects?owned=true&visibility=public',
      headers: {
        Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects
      }
    })
      .then(res => res.data)
      .catch(function (err) {
        console.error(err)
        return null
      })
  },
  getProjectById: async function (projectId: number) {
    return await axios({
      method: 'get',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/' + projectId,
      headers: {
        Authorization: 'Bearer ' + config.gitlab.token_readWriteProjects
      }
    })
      .then(res => res.data)
      .catch(function (err) {
        console.error(err)
        return null
      })
  },
  getProjectPipelineLatestStatus: async function (projectId: number) {
    return await axios({
      method: 'get',
      url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/' + projectId + '/pipelines'
    })
      .then(res => res.data[0].status)
      .catch(function (err) {
        console.error(err)
        return null
      })
  }
}

export {gitlabController}