<template>
  <div v-if="showCard" class="error-overview-container">
    <div class="error-overview">

      <div class="error-section">
        <div class="error-label">Method</div>
        <div class="error-value">
          <basic-badge :icon="requestIcon" :variant="badgeVariant">{{ requestMethod || 'UNKNOWN' }}</basic-badge>
        </div>
      </div>

      <div class="error-section">
        <div class="error-label">Url</div>
        <div class="error-value">
          {{ url }}
        </div>
      </div>

      <div class="error-section">
        <div class="error-label">Response expected</div>
        <div class="error-value">
          {{ expectResponse }}
        </div>
      </div>

      <div class="error-section" v-if="expectedException">
        <div class="error-label">Expected exceptions</div>
        <div class="error-value">
          {{ expectedException }}
        </div>
      </div>

      <div class="error-section">
        <div class="error-label">Header</div>
        <div class="error-value">
<!--          <div class="json-display">-->
<!--            {{ formattedHeader }}-->
<!--          </div>-->
          <div class="error-sub-section-container">
            <div class="error-sub-section" v-for="(value, key) in params.headers" :key="key">
              <div class="error-sub-label"> {{ key }}</div>
              <div class="error-sub-value"> {{ value }}</div>
            </div>
          </div>
        </div>
      </div>


      <div class="error-section">
        <div class="error-label">Body</div>
        <div class="error-value">
          <div class="json-display">
            {{ formattedBody }}
          </div>
        </div>
      </div>


    </div>
  </div>
  <div v-else class="no-data">
    <span class="space-around">No request data available</span>
  </div>

</template>

<script>

import BasicButton from "@/components/UI/BasicButton.vue";
import BasicBadge from "@/components/UI/BasicBadge.vue";


export default {
  name: "ErrorModalRequest",
  components: {BasicBadge, BasicButton},
  props: {
    error: {
      type: Object,
      required: true
    }
  },
  computed: {
    expectedException() {
      return this.request.expectedException
    },
    expectResponse() {
      return this.request.expectResponse
    },
    showCard() {
      return (this.error != null && this.error.request != null);
    },
    request() {
      return JSON.parse(this.error.request);
    },
    url() {
      return this.request.url;
    },
    params() {
      return this.request.params;
    },
    requestMethod() {
      return this.params.method;
    },
    requestIcon() {
      switch (this.requestMethod) {
        case "GET":
          return "download";
        case "POST":
        case "PUT":
          return "upload";
        case "DELETE":
          return "trash";
        default:
          return ""
      }
    },
    header() {
      return this.params.headers;
    },
    body() {
      return this.params.body;
    },
    badgeVariant() {
      switch (this.requestMethod) {
        case "GET":
          return "primary";
        case "POST":
        case "PUT":
          return "secondary";
        case "DELETE":
        default:
          return "grey";
      }
    },
    formattedBody() {
      if (!this.body) return '[]';
      return JSON.stringify(JSON.parse(this.body), null, 2);
    },
    formattedHeader() {
      if (!this.header) return '';
      return JSON.stringify(this.header, null, 2);
    }
  }

}
</script>


<style scoped>
.error-overview-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.error-overview {
  overflow: auto;
  padding: 1.6rem;
  display: flex;
  flex-direction: column;
  gap: 1.6rem;
  flex: 1;
  min-height: 0;
}

.error-section {
  display: grid;
  grid-template-columns: 20rem 1fr;
  align-items: center;
  padding: 0.8rem;
  gap: 0.6rem;
  border-bottom: .1rem solid #E3EDFF
}

.error-sub-section-container {


  width: fit-content;
}

.error-section:last-child {
  border-bottom: none;
}

.error-label {
  align-self: start;
  font-weight: 500;
  font-size: 1.4rem;
  color: #6B869C;
  text-transform: uppercase;
  letter-spacing: 0.05rem;
}

.error-sub-label {
  color: #6B869C;
  font-weight: 500;
  font-size: 1.4rem;
  text-transform: uppercase;
}

.error-sub-value {
  font-size: 1.2rem;
  padding-left: 1.2rem;
  padding-bottom: 0.8rem;
}

.error-value {
  color: #6b7280;
  font-size: 1.4rem;
  word-break: break-word;
}

.no-data {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.6rem;
}

.space-around {
  margin: 3rem;
}

.json-display {
  margin: 0;
  font-family: 'Roboto mono', monospace;
  font-size: 1.2rem;
  flex: 1;
  color: #6b7280;
  white-space: pre-wrap;
  word-break: break-word;
  height: 100%
}
</style>