import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useRef} from 'react';
import {
  Image,
  StyleSheet,
  Text,
  TouchableOpacity,
  TouchableWithoutFeedback,
  View,
} from 'react-native';
import {ProfilePreviewType, ScreenType, UserType} from '../../types';
import {normalize} from '../../utils';
import Avatar from '../../components/common/Avatar';
import {navigateToProfile} from '../../utils/users';
import {useDispatch, useSelector} from 'react-redux';
import {RootState} from 'src/store/rootReducer';

interface TaggDraggableProps {
  taggedUser: ProfilePreviewType;
  editingView: boolean;
  deleteFromList: () => void;
  setStart: Function;
}

const TaggDraggable: React.FC<TaggDraggableProps> = (
  props: TaggDraggableProps,
) => {
  const navigation = useNavigation();

  const gotoTaggedProfile = (userID: string) => {
    //Since the logged In User is navigating to own profile, useXId is not required
    navigation.navigate('Profile', {
      ScreenType,
      userXId: userID,
    });
  };

  const {taggedUser, editingView, deleteFromList, setStart} = props;
  const state = useSelector((rs: RootState) => rs);

  const dispatch = useDispatch();
  let uriX = require('../../assets/images/draggableX.png');
  let uriTip = require('../../assets/images/Tagg-Triangle.png');

  const draggableRef = useRef(null);
  useEffect(() => {
    draggableRef.current.measure((fx, fy, width, height, px, py) => {
      console.log('Drag Component width is: ' + width);
      console.log('Drag Component height is: ' + height);
      console.log('X offset to frame: ' + fx);
      console.log('Y offset to frame: ' + fy);
      console.log('X offset to page: ' + px);
      console.log('Y offset to page: ' + py);
      setStart([width, height]);
    });
  }, []);

  const user: UserType = {
    userId: taggedUser.id,
    username: taggedUser.username,
  };

  return (
    <TouchableWithoutFeedback>
      <View
        style={{
          height: 55,
          flexDirection: 'column',
          alignItems: 'center',
          borderWidth: 1,
        }}>
        <Image style={styles.imageTip} source={uriTip} />
        <View style={styles.container}>
          <TouchableOpacity
            onPressIn={() =>
              navigateToProfile(
                state,
                dispatch,
                navigation,
                ScreenType.Profile,
                user,
              )
            }
            disabled={editingView}
            style={[styles.button]}
            ref={draggableRef}>
            <Avatar style={styles.avatar} uri={taggedUser.thumbnail_url} />
            <Text
              style={editingView ? styles.buttonTitle : styles.buttonTitleX}>
              @{taggedUser.username}
            </Text>
            {editingView && (
              <TouchableOpacity
                disabled={false}
                onPressIn={() => deleteFromList()}>
                <Image style={styles.imageX} source={uriX} />
              </TouchableOpacity>
            )}
          </TouchableOpacity>
        </View>
      </View>
    </TouchableWithoutFeedback>
  );
};

const styles = StyleSheet.create({
  container: {
    borderWidth: 1,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
  },
  buttonTitle: {
    borderWidth: 1,
    color: 'white',
    fontSize: normalize(11),
    fontWeight: '700',
    lineHeight: normalize(13.13),
    letterSpacing: normalize(0.6),
    paddingHorizontal: '1%',
  },
  buttonTitleX: {
    color: 'white',
    fontSize: normalize(11),
    fontWeight: '700',
    lineHeight: normalize(13.13),
    letterSpacing: normalize(0.6),
    paddingHorizontal: '1%',
    marginRight: '-10%',
  },
  avatar: {
    borderWidth: 1,
    borderRadius: 100 / 2,
    overflow: 'hidden',
    marginLeft: '4%',
    marginRight: '-1%',
    flex: 0.45,
    aspectRatio: 1,
  },
  imageX: {
    borderWidth: 1,
    width: normalize(15),
    height: normalize(15),
  },
  imageTip: {
    borderWidth: 1,
    bottom: -5,
    width: normalize(15),
    height: normalize(15),
  },
  button: {
    borderWidth: 1,
    paddingTop: 3,
    paddingBottom: 3,
    justifyContent: 'space-evenly',
    alignItems: 'center',
    borderRadius: 20,
    flexDirection: 'row',
    backgroundColor: 'rgba(0, 0, 0, 0.8)',
  },
});

export default TaggDraggable;
