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 { WalkPathData } from '../types';
import { mapEventsPropType, LatLngPropType } from '../prop-types';

export interface WalkPathProp {
    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: WalkPathData }) => void;
}
const events = ['onSearchStart', 'onSearchComplete'];

export class WalkPath extends Component<WalkPathProp> {
    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 = 'WalkPath';

    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 <AWalkPath {...props} />;
    }
}

// @ts-ignore
const AWalkPath = requireNativeComponent('WalkPath', WalkPath);
