import React from 'react';
import { View, Text } from 'react-native';
import { useAppTheme } from '@theme';
import { PrimaryButton } from '@components';
import { changeOrderStatusThunk, useAppDispatch, useAppSelector } from '@store';
import { advanceJobStatus } from '@store/commonReducers/job';
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { AppStackParamList } from '@navigation/navigationTypes';
import { RouteNames, JobStatus } from '@utils/constants';
import { getStyles } from './orderPickedUpScreen.styles';

export const OrderPickedUpScreen: React.FC = () => {
  const { colors } = useAppTheme();
  const styles = getStyles(colors);
  const dispatch = useAppDispatch();
  const route =
    useRoute<RouteProp<AppStackParamList, RouteNames.OrderPickedUp>>();
  const { jobId } = route.params;
  // console.log(jobId, 'jobId');
  const navigation =
    useNavigation<NativeStackNavigationProp<AppStackParamList>>();

  // const activeJob = useAppSelector(state => state.job.activeJob);
  const { activeDeliveries } = useAppSelector(state => state.deliveries);
  const activeJob = activeDeliveries?.delivery;

  const handleStartDelivery = () => {
    dispatch(
      changeOrderStatusThunk({ deliveryId: jobId, action: JobStatus.PickedUp }),
    );
    navigation.navigate(RouteNames.LiveTracking, {
      jobId: jobId,
    });
  };

  if (!activeJob) return null;

  return (
    <View style={styles.container}>
      {/* Map Background Placeholder */}
      <View style={styles.mapPlaceholder}>
        <Text style={styles.placeholderText}>[ Map Placeholder ]</Text>
      </View>

      {/* Deliver Bottom Card */}
      <View style={styles.bottomCard}>
        <Text style={styles.cardTitle}>Deliver to Customer</Text>
        <Text style={styles.customerName}>
          {activeJob?.order?.customer?.user?.name}
        </Text>
        <Text style={styles.customerAddress}>
          {activeJob?.order?.dropAddress?.houseNumber},{' '}
          {activeJob?.order?.dropAddress?.addressLine1}
          {activeJob?.order?.dropAddress?.city},{' '}
          {activeJob?.order?.dropAddress?.state},
          {activeJob?.order?.dropAddress?.postalCode}
          {'\n'}
          {activeJob?.order?.customer?.user?.phone}
        </Text>

        <PrimaryButton
          title="Start Delivery"
          onPress={handleStartDelivery}
          style={styles.startButton}
        />
      </View>
    </View>
  );
};

export default OrderPickedUpScreen;
