<template>
  <div class="duck-terminal">
    <div class="terminal-header">
      <div class="terminal-buttons">
        <span class="terminal-button close"></span>
        <span class="terminal-button minimize"></span>
        <span class="terminal-button maximize"></span>
      </div>
      <div class="terminal-title">portfolio@duck:~</div>
    </div>
    <div class="terminal-body">
      <div class="terminal-content" ref="terminalContent">
        <div
          class="terminal-line"
          v-for="(line, index) in displayedLines"
          :key="index"
        >
          <template v-if="line.type === 'command'">
            <span class="terminal-prompt">portfolio@duck:~$</span>
            <span class="terminal-text">{{ line.text }}</span>
          </template>
          <template v-else-if="line.type === 'skills'">
            <div class="terminal-output skills-grid">
              <div class="skill-item" v-for="(skill, skillIndex) in line.skills" :key="skillIndex">
                {{ skill }}
              </div>
            </div>
          </template>
          <template v-else>
            <span class="terminal-output">{{ line.text }}</span>
          </template>
        </div>
        <div
          class="terminal-line typing-line"
          v-if="showCursor"
        >
          <span class="terminal-prompt">portfolio@duck:~$</span>
          <span class="terminal-text typing">{{ currentText }}</span>
          <span class="terminal-cursor"></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "DuckTerminal",
  data() {
    return {
      terminalLines: [
        { type: "command", text: "cat profil.txt" },
        {
          type: "output",
          text: "Développeur PHP et Admin Sys/Réseau avec passion pour le web",
        },
        { type: "command", text: "cat experience.txt" },
        {
          type: "output",
          text: "Plus de 4 ans d'expérience dans différents contextes professionnels",
        },
        { type: "command", text: "ls -la skills/" },
        {
          type: "skills",
          skills: [
            "PHP", "Laravel", "MySQL", "Linux",
            "Symfony", "Docker", "Git", "Networking",
            "JavaScript", "REST API", "DevOps", "Cloud"
          ]
        },
        { type: "command", text: "whoami" },
        {
          type: "output",
          text: "Expert en développement PHP et en administration système/réseau",
        },
      ],
      displayedLines: [],
      currentLineIndex: 0,
      currentText: "",
      currentCharIndex: 0,
      typingSpeed: 40,
      commandDelay: 300,
      outputDelay: 300,
      showCursor: false
    };
  },
  mounted() {
    setTimeout(() => {
      this.startTyping();
    }, 500);
  },
  methods: {
    scrollToBottom() {
      if (this.$refs.terminalContent) {
        this.$refs.terminalContent.scrollTop = this.$refs.terminalContent.scrollHeight;
      }
    },
    startTyping() {
      this.processNextLine();
    },
    processNextLine() {
      if (this.currentLineIndex >= this.terminalLines.length) {
        return;
      }

      const currentLine = this.terminalLines[this.currentLineIndex];

      if (currentLine.type === "command") {
        // For command lines, animate typing
        this.showCursor = true;
        this.currentText = "";
        this.currentCharIndex = 0;
        this.typeCurrentLine();
      } else {
        // For output and skills lines, just add them after a delay
        this.showCursor = false;
        setTimeout(() => {
          this.displayedLines.push(currentLine);
          this.currentLineIndex++;
          this.scrollToBottom();
          setTimeout(() => {
            this.processNextLine();
          }, 200);
        }, this.outputDelay);
      }
    },
    typeCurrentLine() {
      if (!this.showCursor || this.currentLineIndex >= this.terminalLines.length) {
        return;
      }

      const currentLine = this.terminalLines[this.currentLineIndex];

      if (this.currentCharIndex < currentLine.text.length) {
        // Still typing the current character
        this.currentText = currentLine.text.substring(
          0,
          this.currentCharIndex + 1
        );
        this.currentCharIndex++;
        setTimeout(this.typeCurrentLine, this.typingSpeed);
      } else {
        // Finished typing this command
        setTimeout(() => {
          // Add the completed command to displayed lines
          this.displayedLines.push({ ...currentLine });
          this.showCursor = false;
          this.currentLineIndex++;
          this.scrollToBottom();
          
          // Wait a bit before processing the next line
          setTimeout(this.processNextLine, this.commandDelay);
        }, 300);
      }
    }
  },
  updated() {
    this.scrollToBottom();
  }
};
</script>

<style>
.duck-terminal {
  width: 100%;
  max-width: 620px;
  background-color: #1a1b26;
  border-radius: 8px;
  overflow: hidden;
  font-family: "Courier New", monospace;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  border: 1px solid #30374b;
  margin-bottom: 2rem;
  height: 320px;
}

.terminal-header {
  height: 36px;
  background-color: #24283b;
  display: flex;
  align-items: center;
  padding: 0 12px;
  border-bottom: 1px solid #30374b;
}

.terminal-buttons {
  display: flex;
  gap: 8px;
}

.terminal-button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.terminal-button.close {
  background-color: #ff5f57;
}

.terminal-button.minimize {
  background-color: #febc2e;
}

.terminal-button.maximize {
  background-color: #28c840;
}

.terminal-title {
  flex-grow: 1;
  text-align: center;
  color: #a9b1d6;
  font-size: 0.875rem;
}

.terminal-body {
  height: calc(100% - 36px);
  position: relative;
  overflow: hidden;
}

.terminal-content {
  padding: 1.25rem;
  height: 100%;
  overflow-y: auto;
  color: #a9b1d6;
  scrollbar-width: thin;
  scrollbar-color: #30374b #1a1b26;
  text-align: left;
}

.terminal-content::-webkit-scrollbar {
  width: 8px;
}

.terminal-content::-webkit-scrollbar-track {
  background: #1a1b26;
}

.terminal-content::-webkit-scrollbar-thumb {
  background-color: #30374b;
  border-radius: 4px;
}

.terminal-line {
  line-height: 1.6;
  white-space: pre-wrap;
  font-size: 1rem;
  margin-bottom: 0.5rem;
  text-align: left;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
}

.terminal-prompt {
  color: #ffdb58;
  margin-right: 8px;
}

.terminal-text {
  color: #a9b1d6;
}

.terminal-output {
  color: #7aa2f7;
  text-align: left;
  width: 100%;
}

.skills-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 0.5rem;
  margin-top: 0.25rem;
  text-align: left;
}

.skill-item {
  padding-right: 1rem;
  text-align: left;
}

.terminal-cursor {
  display: inline-block;
  width: 8px;
  height: 16px;
  background-color: #7aa2f7;
  animation: blink 1s step-start infinite;
  vertical-align: middle;
}

@keyframes blink {
  0%,
  50% {
    opacity: 1;
  }
  51%,
  100% {
    opacity: 0;
  }
}

.typing-line {
  display: flex;
  align-items: center;
}

.typing {
  margin-right: 2px;
}

@media (max-width: 768px) {
  .duck-terminal {
    width: 100%;
    min-width: auto;
  }
  
  .skills-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>