<template>
  <ul class="route-list" :class="pointType" :isLast="isLast ? 'true': 'false'">
    <template v-for="(point, index) in list">
      <li
        class="expandable-button"
        :key="-index"
        v-if="shouldShowExpandable(index, list)"
      >
        <button :data-id="dataId + '-collapsible-btn'" @click="toggleExpandable">
          <span>Ver mas ({{list.length-2}})</span>
          <span class="icon icon-arrow-up" :class="isExpanded ? 'is-reverse' : ''"></span>
        </button>
        <hr>
      </li>
      <li
        class="route-point-item"
        :data-id="dataId + '-list-element-' + index"
        :key="point.index"
        v-if="isExpanded &&
        isOnCollapsible(index, list) ||
        !isOnCollapsible(index, list)"
      >
        <div class="point-data">
          <time :datetime="point.arrivalDate">{{ point.arrivalDate }}</time>
          <p class="point-name">{{ point.id }}</p>
          <p class="point-address">{{ JSON.stringify(point.coordinates) }}</p>
        </div>
        <div :class="point.bulletClass">
          <span class="route-bullet">{{point.index}}</span>
        </div>
      </li>
    </template>
  </ul>
</template>

<script>
export default {
  name: 'route-list-expandable',
  props: {
    dataId: {
      type: String,
      default: 'point-list',
    },
    list: {
      type: Array,
      default: () => [],
    },
    pointType: {type: String},
    isLast: {type: Boolean},
    total: {type: Number},
  },
  data() {
    return {isExpanded: false};
  },
  methods: {
    isOnCollapsible(index, list) {
      return (
        list.length > 2
        && index > 0
        && index < list.length - 1
        && this.total > 4
      );
    },
    shouldShowExpandable(index, list) {
      return (
        list.length > 2
        && index === 1
        && index < list.length - 1
        && this.total > 4
      );
    },
    toggleExpandable() {
      this.isExpanded = !this.isExpanded;
    },
  },
};
</script>

<style lang="scss" scoped>
@mixin big-bullet {
  position: absolute;
  display: flex;
  left: -16px;
  top: -10px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
}

@mixin small-bullet {
  position: absolute;
  top: -1px;
  left: -7px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

ul.past li {
  border-left: 3px solid $--color-primary;
}

ul.future li {
  border-left: 3px dashed $--color-primary;
}

ul.past[isLast="true"] li:last-of-type,
ul.future li:last-of-type {
  border: none;
}

ul.past[isLast="false"] li:last-of-type {
  border-left: 3px dashed $--color-primary;
}

ul.future li:last-of-type .destiny-bullet,
ul.past[isLast="true"] li:last-of-type .origin-bullet {
  left: -4px;
}

ul.future .point-data {
  opacity: 0.4;
}

.expandable-button {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;

  hr {
    width: 100%;
    height: 1px;
    position: absolute;
    margin: 0;
    color: rgba(216, 216, 216, 0.5);
    background-color: rgba(216, 216, 216, 0.5);
  }

  button {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 12px;

    z-index: 1;
    border: none;
    border-radius: 4px;
    background-color: $--color-white;
    box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.1);

    text-align: center;
    text-transform: uppercase;
    font-size: $size-xs;
    font-weight: $font-weight-bold;
    line-height: $size-l;
    color: $--color-primary;

    .icon {
      font-family: "onesait-icons";
      transition: all 0.3s;
      font-size: $size-l;
      font-weight: $font-weight-bold;
      line-height: $size-l;

      &-arrow-up::before {
        content: "c";
      }

      &.is-reverse {
        transform: rotate(180deg);
        transform-origin: center;
      }
    }
  }
}

li {
  position: relative;
  padding-bottom: 40px;
  padding-left: 40px;

  .point-name {
    font-size: $size-ml;
    line-height: $size-l;
    padding: 4px 0;
  }
  .point-address {
    padding: 4px 0;
    line-height: 24px;
    font-size: $size-ml;
    font-weight: $font-weight-medium;
  }

  time {
    font-size: $size-xs;
    margin-bottom: 10px;
  }
}

.origin-bullet {
  @include small-bullet;
  background-color: $--color-primary;
}
.past-bullet {
  @include big-bullet;
  background-color: $--color-primary;
}
.future-bullet {
  @include big-bullet;
  border: 2px solid $--color-secondary;
  background-color: $--color-white;
}
.destiny-bullet {
  @include small-bullet;
  border: 2px solid $--color-secondary;
  background-color: $--color-white;
}

.future-bullet .route-bullet {
  color: $--color-primary;
}
.past-bullet .route-bullet {
  color: $--color-white;
}
.destiny-bullet .route-bullet,
.origin-bullet .route-bullet {
  display: none;
}
.route-bullet {
  font-size: $size-xs;
}
</style>
