import React, { useState, Fragment, useCallback, useEffect, useRef, useLayoutEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import Styled from 'styled-components/native';
import Theme from '~/Themes';
import { showSelectorAction } from '~/GlobalReducers/phone';
import { WebView } from 'react-native-webview';
import { Alert, ActivityIndicator, Image, View, TouchableOpacity, Modal, Platform, Linking, BackHandler, Keyboard } from 'react-native';
import { useToast } from './Message';
import { fetchPost } from '~/Utils/fetch';
import FileSvg from '~/Assets/Svgs/file_text.svg';
import Icon from './Icon';
import { Util } from '~/Utils/utils';

export const usePhone = () => {

    const dispatch = useDispatch();
    const selectorPayload = useSelector(({GlobalPhone}) => GlobalPhone.selector);
    const showSelector = useCallback((payload) => dispatch(showSelectorAction(payload)), []);

    const showCallOrMessageSelector = (phoneNumber) => {
        const number = (phoneNumber+'').replace(/[^0-9]/g, '');
        if(number) {
            showSelector({...selectorPayload, showSelector:true, phoneNumber:number});
        }
    }

    return { showCallOrMessageSelector:showCallOrMessageSelector };
}

const ProviderBackground = Styled.TouchableOpacity`
    position: absolute;
    left:0; top:0; right:0; bottom:0;
    background-color: black;
    opacity: 0.6;
`;
const ProviderContainer = Styled.View`
    position: absolute;
    left:0; top:0; right:0; bottom:0;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
`;
const ProviderContent = Styled.View`
    margin-bottom: 50px;
    width: 100%;
    height: 200px;
    flex-direction: row;
    align-items: center;
    justify-content: center;
`;
const Item = Styled.TouchableOpacity`
    width: 150px;
    height: 150px;
    background-color: white;
    border-radius: 5px;
    overflow: hidden;
    margin: 10px;
    align-items: center;
    justify-content: center;
`;
const ItemText = Styled.Text`
    font-family: ${Theme.Fonts.Regular};
    font-size: 18;
    line-height: 22;
    include-font-padding: false;
    margin-top: 20px;
`;

export const PhoneProvider = () => {
    const payload = useSelector(({GlobalPhone}) => GlobalPhone.selector);
    const dispatch = useDispatch();
    const showSelector = useCallback((payload) => dispatch(showSelectorAction(payload)), []);
    const backHandler = useRef(null);

    if(payload.showSelector) {
        Keyboard.dismiss();
    }

    useLayoutEffect(() => {
        if(backHandler.current != null) backHandler.current.remove();

        backHandler.current = BackHandler.addEventListener('hardwareBackPress', () => {
            showSelector({
                ...payload,
                showSelector: false
            });

            if(payload.showSelector) {
                return true;
            } else {
                return false;
            }
        });
    }, [payload]);

    return (
        payload.showSelector && (
        <>
            <ProviderBackground onPress={() => {
                showSelector({
                    ...payload,
                    showSelector: false
                });
            }} />
            <ProviderContainer>
                <ProviderContent>
                    <Item onPress={() => { 
                        Linking.openURL(`tel:${payload.phoneNumber}`); 
                        payload.onSelect('phone'); 
                        showSelector({ ...payload, showSelector: false }); 
                    }}>
                        <Icon iconFamily="AntDesign" iconName="phone" size={50} color="black" />
                        <ItemText>전화</ItemText>
                    </Item>
                    <Item onPress={() => { 
                        Linking.openURL(`sms:${payload.phoneNumber}`); 
                        payload.onSelect('sms'); 
                        showSelector({ ...payload, showSelector: false }); 
                    }}>
                        <Icon iconFamily="AntDesign" iconName="message1" size={50} color="black" />
                        <ItemText>SMS</ItemText>
                    </Item>
                </ProviderContent>
            </ProviderContainer>
        </>
        )
    )
}


