<template>
  <div class="results" :class="getDisplayClass()">
    <div class="journey" v-for="connection in this.connections" :key="connections.indexOf(connection)"><div>
      <h2>{{ toTime(connection.legs[0].departure) }} - {{ toTime(connection.legs[connection.legs.length -1].arrival) }}</h2>
      <div class="leg" v-for="leg in connection.legs" :key="connection.legs.indexOf(leg)" :class="leg.walking ? 'walking' : null" :style="{backgroundColor:getTimeColor(leg.arrival), background:getGradient(leg.departure, leg.arrival)}" >
        <div class="linename" >
          <div class="linenameinner" :style="{backgroundColor:getLineColors(leg)[1], color:getLineColors(leg)[0]}">{{ getRouteNumber(leg) }}</div>
          <div class="linenameinner" v-if="isLarge" :style="{backgroundColor:getLineColors(leg)[1], color:getLineColors(leg)[0]}">{{ leg.direction }}</div>
          </div>
        <div class="lineinfo">
          <div class="station" >{{ leg.origin.name }}<br/>{{ timeWithDelay(leg.departure, leg.departureDelay) }}<p v-if="leg.departurePlatform"> Gleis {{ leg.departurePlatform }} </p></div>
          <div class="line" v-if="isLarge">
            <p class="zugnummer">{{ leg.line?.fahrtNr }}</p>
            <p class="operator" v-if="!getOperatorLogo(leg.line?.operator)">{{ leg.line?.operator?.name }}</p>
            <img class="operator-logo" :src="getOperatorLogo(leg.line?.operator)" v-if="getOperatorLogo(leg.line?.operator)">
          </div>
          <div class="station">{{ leg.destination.name }}<br/>{{ timeWithDelay(leg.arrival, leg.arrivalDelay) }} <p v-if="leg.arrivalPlatform">Gleis {{ leg.arrivalPlatform }}</p></div>
        </div>
        <div class="displaywide" v-if="isLarge">
          <div v-for="remark in leg.remarks" :key="leg.remarks.indexOf(remark)">
            <img class="remark-icon" :src="getIconForRemark(remark)" v-if="getIconForRemark(remark)" :title="$t('routing.remarks.'+remark.code)">
          </div>
        </div>
        <div v-if="isLarge">
          <div v-for="remark in leg.remarks" :key="leg.remarks.indexOf(remark)">
            <p v-if="!getIconForRemark(remark)">{{remark.text}}</p>
          </div>
        </div>
        <hr/>
      </div>
    </div></div>
  </div>
</template>

<script>
  import timefunctions from '@/lib/time.js'
  import routefunctions from '@/lib/routes.js'
  import operatorfunctions from '@/lib/operators.js'
  import remarkfunctions from '@/lib/remarks.js'

  export default {
    name: 'RoutingBahn',
    props: {
      connections: Array,
      isMobile: Boolean,
    },
    components: {
    },
    data() {
        return {
            from: {name:"", id:null},
            to: {name:"", id:null},
            isLarge: true,
        }
    },
    methods: {
      getDisplayClass(){
        return this.isMobile ? "displaytall" : "displaywide";
      },
      toDate(string){
        return new Date(string).toLocaleDateString("de-DE");
      },
      timeWithDelay(actual, delay){
        let time = this.toTime(actual);
        if (delay > 30){
          return time + " +" + Math.ceil(delay/60.0);
        }
        return time;
      },
      toTime(string){
        return new Date(string).toLocaleTimeString("de-DE", {timeStyle: 'short'});
      },
      getGradient(time1, time2){
        return "linear-gradient(0deg,"+timefunctions.getTimeColor(time2)+","+timefunctions.getTimeColor(time1)+")";
      },
      getTimeColor(string) {
        timefunctions.getTimeColor(string)
      },
      getLineColors(line){
        return routefunctions.getLineColors(line)
      },
      getRouteNumber(leg){
        return routefunctions.getRouteNumber(leg)
      },
      getOperatorLogo(line){
        return operatorfunctions.getOperatorLogo(line)
      },
      getIconForRemark(remark){
        return remarkfunctions.getIconForRemark(remark)
      },
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .displaywide {
    display: inline-flex;
  }
  .displaytall {
    display: block;
  }
  .journey {
    margin-left: auto;
    margin-right: auto;
    flex: 1;
    min-width: 500px;
  }
  .journey > div{
    margin: 40px;
  }
  .station {
    margin: 4px;
    display: inline-block;
    width:130px;
    vertical-align: text-top;
  }
  .line {
    margin: 4px;
    display: inline-block;
    width:120px;
    vertical-align: text-top;
    align-content: center;
  }
  .linename {
    width:100px;
    font-family:"Raveo-display-bold";
    align-content: center;
    display:inline-flex;
    width:max-content;
  }
  .linenameinner {
    padding-left: 10px;
    padding-right: 10px;
    margin: 15px;
  }

  .operator {
    font-size: 10px;
    margin:10px;
  }
  .operator-logo {
    margin:10px;
    max-width:100px;
    max-height:40px;
  }
  div {
    font-family:"Raveo-display-medium";
  }
  .walking > div {
    font-size: 12px;
  }
  @font-face {
    font-family: "Raveo-display-bold";
    src: url("../assets/Raveo Display Bold.woff2") format("opentype"); 
  }
  @font-face {
    font-family: "Raveo-display-medium";
    src: url("../assets/Raveo Display Medium.woff2") format("opentype"); 
  }
</style>
