import { UpdateServerRequestModule } from '@/api/RequestModules';
import { DataModel } from '@imengyu/js-request-transform';

export class RouteApi extends UpdateServerRequestModule<DataModel> {
  constructor() {
    super();
  }

  async getRouteInfo(outlinkId: number) {
    return (await this.get<RouteInfo>('/scenic-spot-route/get-by-outlink-id/' + outlinkId, '获取路线信息')).data;
  }
}

export interface RouteInfo {
  id: number;
  intro: string;
  outlinkId: number;
  items: {
    id: number;
    atDay: number;
    costDays: number;
    order: number;
    routeId: number;
    scenicSpot: {
      id: number;
      name: string;
      address: string;
      latitude: number;
      longitude: number;
      image: string;
      images: string[];
      intro: string;
      outlinkId: number;
    },
    scenicSpotId: number;
    toNextRoute: {
      bicycling: RouteToNextRouteData;
      driving: RouteToNextRouteData;
      walking: RouteToNextRouteData;
      ebicycling: RouteToNextRouteData;
      transit: {
        distance: number
        duration: number;
        steps: ({
          direction: string;
          distance: number;
          duration: number;
          mode: 'WALKING';
          polyline: number[];
          steps: RouteToNextRouteData;
        }|{
          mode: 'TRANSIT';
          lines: {
            destination: {id: string, title: string};
            distance: number;
            duration: number;
            end_time: string;
            getoff: {id: string, title: string, location: {lat: number, lng: number}};
            geton: {id: string, title: string, location: {lat: number, lng: number}};
            id: string;
            polyline: number[];
            price: number;
            running_status: number;
            start_time: string;
            station_count: number;
            stations: {id: string, title: string, location: {lat: number, lng: number}}[];
            title: string;
            vehicle: string;
          }[];
        })[];
      };
    };
    toNextBestWay: '' | RouteToNextBestWay;
  }[];
}
export type RouteToNextBestWay = 'driving' | 'walking' | 'bicycling' | 'ebicycling' | 'transit';
interface RouteToNextRouteData {
  distance: number
  duration: number;
  polyline: number[];
  steps: {
    act_desc: string;
    dir_desc: string;
    distance: number;
    instruction: string;
    polyline_idx: number[];
    road_class: number;
    road_name: string;
  }[];
}

export function formatDistance(distance: number) {
  if (distance < 1000) {
    return distance + '米';
  } else {
    return (distance / 1000).toFixed(2) + 'km';
  }
}
export function formatDuration(duration: number) {
  if (duration >= 60)
    return Math.floor(duration / 60) + '小时' + (duration % 60) + '分钟';
  return duration + '分钟';
}
export function getRouteToNextBestWayText(way: RouteToNextBestWay|'') {
  switch (way) {
    case 'driving':
      return '🚗 驾车';
    case 'walking':
      return '🚶 步行';
    case 'ebicycling':
      return '🚴 电动车';
    case 'bicycling':
      return '🚴 自行车';
    case 'transit':
      return '🚌 公交';
    default:
      return '🔄 灵活选择';
  }
}

export default new RouteApi();
