<template>
  <div>
    <div class="item-list">
      <div class="item" v-for="run in runs" v-bind:key="run.id" :class="runResultClass(run)">
        <div class="item-content">
          <router-link
            v-if="username"
            tag="div"
            class="name"
            :to="userLocalRunLink(username, run.id)"
          >
            <span>{{run.name}}</span>
          </router-link>
          <router-link
            v-else
            tag="div"
            class="name"
            :to="projectRunLink(ownertype, ownername, projectname, run.id)"
          >
            <span>{{run.name}}</span>
          </router-link>
          <div class="commitmessage">{{run.annotations.message}}</div>
          <span v-if="stillRunning(run)" class="stillrunning tag">Still running</span>
          <span v-if="!stillRunning(run)" class="stillrunning"></span>
          <div class="source-info">
            <a :href="run.annotations.commit_link" class="commit" target="_blank">
              <i class="mdi mdi-source-commit mdi-rotate-90"></i>
              <span>{{run.annotations.commit_sha.substring(0,8)}}</span>
            </a>
            <a
              v-if="run.annotations.event_type == 'push'"
              :href="run.annotations.branch_link"
              class="commit"
              target="_blank"
            >
              <i class="mdi mdi-source-branch"></i>
              <span>{{run.annotations.branch}}</span>
            </a>
            <a
              v-else-if="run.annotations.event_type == 'tag'"
              :href="run.annotations.tag_link"
              class="commit"
              target="_blank"
            >
              <i class="mdi mdi-tag"></i>
              <span>{{run.annotations.tag}}</span>
            </a>
            <a
              v-else-if="run.annotations.event_type == 'pull_request'"
              :href="run.annotations.pull_request_link"
              class="commit"
              target="_blank"
            >
              <i class="mdi mdi-source-pull"></i>
              <span>PR #{{run.annotations.pull_request_id}}</span>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiurl, fetch } from "@/util/auth";
import { userLocalRunLink, projectRunLink } from "@/util/link.js";

export default {
  components: {},
  name: "runs",
  props: {
    ownertype: String,
    ownername: String,
    username: String,
    projectname: String
  },
  data() {
    return {
      runs: [],
      polling: null,
      project: null,
      user: null
    };
  },
  methods: {
    projectRunLink: projectRunLink,
    userLocalRunLink: userLocalRunLink,
    stillRunning(run) {
      return run.result != "unknown" && run.phase == "running";
    },
    runResultClass(run) {
      if (run.result == "unknown") {
        if (run.phase == "queued") return "unknown";
        if (run.phase == "cancelled") return "failed";
        if (run.phase == "running") return "running";
      }
      if (run.result == "success") return "success";
      if (run.result == "failed") return "failed";
      if (run.result == "stopped") return "failed";
      return "unknown";
    },
    fetchProjectRuns() {
      fetch(
        apiurl(
          "/projects/" +
            this.ownertype +
            "/" +
            this.ownername +
            "/" +
            this.projectname
        )
      )
        .then(res => res.json())
        .then(res => {
          console.log(res);
          this.project = res;
          console.log("project", this.project);

          this.fetchRuns();
        });
    },
    fetchUserRuns() {
      fetch(apiurl("/users/" + this.username))
        .then(res => res.json())
        .then(res => {
          console.log(res);
          this.user = res;
          console.log("user", this.user);

          this.fetchRuns();
        });
    },
    fetchRuns() {
      let u = apiurl("/runs");
      //console.log("this.project.id", this.project.id);
      console.log("u", u);
      if (this.project !== null) {
        u.searchParams.append("group", this.project.id);
      } else if (this.user !== null) {
        u.searchParams.append("group", this.user.id);
      }

      fetch(u)
        .then(res => res.json())
        .then(res => {
          console.log(res);
          let runs = res.runs.map(function(run) {
            return run;
          });
          this.runs = runs;
          console.log("runs", this.runs);
        });
    },
    pollData() {
      this.polling = setInterval(() => {
        this.fetchRuns();
      }, 2000);
    }
  },
  created: function() {
    console.log("username", this.username);
    console.log("projectname", this.projectname);
    if (this.projectname !== undefined) {
      this.fetchProjectRuns();
    } else if (this.username !== undefined) {
      this.fetchUserRuns();
    } else {
      this.fetchRuns();
    }
    this.pollData();
  },
  beforeDestroy() {
    clearInterval(this.polling);
  }
};
</script>

<style scoped lang="scss">
@import "@/css/_variables.scss";

.project-title {
  display: flex;
  align-items: center;
  padding-left: 5px;
  margin-bottom: 25px;
  .project-name {
    padding-left: 5px;
    font-size: 1.5rem;
    padding-right: 1rem;
  }
}

.item-list {
  .item {
  }

  .item-content {
    margin-bottom: 5px;
    border: 1px solid $grey-lighter;
    border-left: 0 solid;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
  }

  .success {
    border-left: 5px solid $green;
  }

  .failed {
    border-left: 5px solid $red;
  }

  .running {
    border-left: 5px solid $blue;
  }

  .unknown {
    border-left: 5px solid $grey-lighter;
  }

  .name {
    flex: 0 0 30%;
    font-weight: bold;
    cursor: pointer;
  }

  .commitmessage {
    flex: 0 0 40%;
  }

  .stillrunning {
    flex: 0 0 10%;
  }

  .source-info {
    flex: 0 0 10%;
    overflow: hidden;
    white-space: nowrap;

    a {
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }

  .commit {
    display: block;
    font-size: 0.8rem;
  }
}
</style>
