import React from 'react';
import * as PropTypes from 'prop-types';
import { ViewPropTypes, requireNativeComponent, ImageSourcePropType, Image } from 'react-native';
import type { LatLng } from '../types';
import Component from '../map-view/component';
import type { RidePathData } from '../types';

import { mapEventsPropType, LatLngPropType } from '../prop-types';

export interface RidePathProp {
    drawPath?: boolean;
    startPoint: LatLng;
    endPoint: LatLng;
    pathWidth?: number;
    pathColor?: string;
    nodeVisible?: boolean;
    startMarkerTitle?: string;
    endMarkerTitle?: string;
    startMarkerIcon?: ImageSourcePropType;
    endMarkerIcon?: ImageSourcePropType;
    nodeMarkerIcon?: ImageSourcePropType;
    throughMarkerIcon?: ImageSourcePropType;
    onSearchStart?: () => void;
    onSearchComplete?: (event: { code: number; path?: RidePathData }) => void;
}
const events = ['onSearchStart', 'onSearchComplete'];

export class RidePath extends Component<RidePathProp> {
    static propTypes = {
        ...ViewPropTypes,
        ...mapEventsPropType(events),
        drawPath: PropTypes.bool,
        startPoint: LatLngPropType.isRequired,
        endPoint: LatLngPropType.isRequired,
        pathWidth: PropTypes.number,
        pathColor: PropTypes.string,
        nodeVisible: PropTypes.bool,
        startMarkerTitle: PropTypes.string,
        endMarkerTitle: PropTypes.string,
        startMarkerIcon: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
        endMarkerIcon: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
        nodeMarkerIcon: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
        throughMarkerIcon: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
    };

    nativeComponent = 'RidePath';

    render() {
        const props: { [key: string]: any } = {
            ...this.props,
            ...this.handlers(events),
        };
        for (let k in props) {
            if (k.indexOf('Icon') > -1) {
                props[k] = Image.resolveAssetSource(props[k]);
            }
        }
        return <ARidePath {...props} />;
    }
}

// @ts-ignore
const ARidePath = requireNativeComponent('RidePath', RidePath);
