<template>
  <div
    class="l-card-grid"
    :style="style"
  >
    <div
      v-for="card in cards"
      :key="card.key"
      class="o-receiving-card"
      :class="card.status"
      :style="card.style"
    >
      <div class="o-receiving-card__status">
        {{ card.tip }}
      </div>
      <i
        v-if="card.dirc"
        class="o-receiving-card__arrow"
        :class="card.dirc"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'ReceivingCardTopology',
  props: {
    info: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      style: null,
      cards: null
    }
  },
  created () {
    this.setDefaults(this.info?.topology?.screenList)
  },
  methods: {
    setDefaults (screenList) {
      let maxRow = 0
      let maxCol = 0
      if (screenList) {
        const cards = []
        screenList.forEach(({ outCardList }) => {
          outCardList.forEach(({ sendCardList }) => {
            sendCardList.forEach(({ rxcList }) => {
              rxcList && rxcList.sort((a, b) => {
                if (a.port === b.port) {
                  return a.sub - b.sub
                }
                return a.port - b.port
              }).forEach(({ port, sub, row_index, col_index }, index) => {
                maxRow = Math.max(maxRow, row_index)
                maxCol = Math.max(maxCol, col_index)
                let tip = sub === 1 ? 'S' : ''
                let dirc = ''
                const next = rxcList[index + 1]
                if (next) {
                  if (port === next.port) {
                    if (row_index > next.row_index) {
                      dirc = 'top'
                    } else if (row_index < next.row_index) {
                      dirc = 'bottom'
                    } else {
                      dirc = col_index < next.col_index ? 'right' : 'left'
                    }
                  } else {
                    tip = 'E'
                  }
                } else if (!tip) {
                  tip = 'E'
                }
                const card = {
                  key: `${port}-${sub}`,
                  style: {
                    gridRow: row_index,
                    gridColumn: col_index
                  },
                  tip, dirc
                }
                cards.push(card)
              })
            })
          })
        })
        this.cards = cards
      }
      this.style = {
        gridTemplateRows: `repeat(${maxRow}, 96px)`,
        gridTemplateColumns: `repeat(${maxCol}, 96px)`
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.l-card-grid {
  display: grid;
  position: relative;
  overflow: auto;
}

.o-receiving-card {
  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  color: $info;
  background: url("~@/assets/screen.png") 0 0 / 100% 100% no-repeat;

  &__status {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 36px;
    height: 36px;
    padding-bottom: 4px;
    color: $black;
    font-size: 14px;
    font-weight: bold;
    background: url("~@/assets/icon_card_unknown.png") 0 0 / 100% 100% no-repeat;
  }

  &__arrow {
    position: absolute;
    z-index: 1;

    &.left {
      transform: rotate(-90deg);
    }

    &.right {
      transform: rotate(90deg);
    }

    &.bottom {
      transform: rotate(180deg);
    }

    &::before {
      content: "";
      position: absolute;
      left: 50%;
      bottom: 20px;
      width: 6px;
      height: 46px;
      background-color: currentColor;
      transform: translateX(-50%);
    }

    &::after {
      content: "";
      position: absolute;
      left: 50%;
      bottom: 66px;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      border-bottom: 10px solid currentColor;
      transform: translateX(-50%);
    }
  }
}
</style>
