import React, { FC, useContext, useEffect, useMemo } from 'react';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import { TrustBannerCallbackParams, TrustBannerProps } from './types';
import styles from './styles';
import {
  ApplicationContext,
  Icon,
  Image,
  MiniAppContext,
  Styles,
  Text,
} from '@momo-kits/foundation';

const defaultData = {
  content: {
    vi: 'An toàn tài sản & Bảo mật thông tin của bạn là ưu tiên hàng đầu của MoMo.',
    en: "Ensuring financial security and data privacy is MoMo's highest priority.",
  },
  subContent: {
    vi: 'Tìm hiểu thêm',
    en: 'Learn more',
  },
  pciImage: 'https://static.momocdn.net/app/img/kits/trustBanner/pci_dss.png',
  sslImage: 'https://static.momocdn.net/app/img/kits/trustBanner/ssl.png',
  momoImage: 'https://static.momocdn.net/app/img/kits/trustBanner/ic_secu.png',
  urlConfig: 'login_and_security',
  icons: [
    'https://static.momocdn.net/app/img/kits/trustBanner/ic_viettinbank.png',
    'https://static.momocdn.net/app/img/kits/trustBanner/ic_agribank.png',
    'https://static.momocdn.net/app/img/kits/trustBanner/ic_vietcombank.png',
    'https://static.momocdn.net/app/img/kits/trustBanner/ic_bidv.png',
  ],
  titleWeb: {
    vi: 'Thông tin',
    en: 'Information',
  },
};

const TrustBanner: FC<TrustBannerProps> = ({
  style,
  screenName = '',
  serviceName = '',
}) => {
  const context = useContext<any>(MiniAppContext);
  const {
    navigator,
    theme,
    translate = (data: { vi: string; en: string }) => {
      return data.vi;
    },
  } = useContext(ApplicationContext);

  const {
    sslImage,
    pciImage,
    momoImage,
    urlConfig,
    content,
    subContent,
    titleWeb,
  } = context?.designSystemConfig?.config?.trustBanner || defaultData;

  const translationSubContent = translate(subContent) || subContent.vi;
  const translationContent = translate(content);

  const trackingParams: TrustBannerCallbackParams = useMemo(
    () => ({
      screen_name: screenName,
      service_name: serviceName,
      component_name: 'logo_trust',
    }),
    [screenName, serviceName],
  );

  useEffect(() => {
    navigator?.maxApi.trackEvent?.(
      'service_component_displayed',
      trackingParams,
    );
  }, [navigator?.maxApi, trackingParams]);

  const _onPress = () => {
    navigator?.maxApi.trackEvent?.('service_component_clicked', {
      ...trackingParams,
      urlConfig,
    });
    if (urlConfig.indexOf('https') > -1) {
      const openWebData = {
        url: urlConfig,
        title: translate(titleWeb) || subContent.vi,
      };
      navigator?.maxApi.openWeb(openWebData);
    } else {
      navigator?.maxApi.startFeatureCode(urlConfig, {}, () => {});
    }
  };

  const renderFooter = () => {
    return (
      <View style={stylesInside.footerContainer}>
        <View style={Styles.row}>
          <Text typography={'action_xs_bold'} color={'#eb2f96'}>
            {translationSubContent}
          </Text>
          <Icon
            source={'arrow_chevron_right_small'}
            color={'#eb2f96'}
            size={20}
          />
        </View>
        <View style={styles.imageList}>
          <Image
            resizeMode={'contain'}
            style={styles.pciImage}
            source={{ uri: pciImage }}
          />
          <Image
            resizeMode={'contain'}
            style={styles.image}
            source={{ uri: sslImage }}
          />
        </View>
      </View>
    );
  };

  return (
    <TouchableOpacity
      onPress={_onPress}
      activeOpacity={0.7}
      style={[style, styles.container]}
    >
      <Image
        style={styles.momoImage}
        resizeMode={'contain'}
        source={{
          uri: momoImage,
        }}
      />
      <View style={styles.flex}>
        <Text
          style={styles.message}
          color={theme.colors.text.secondary}
          typography={'description_default_regular'}
          numberOfLines={2}
        >
          {translationContent}
        </Text>
        {renderFooter()}
      </View>
    </TouchableOpacity>
  );
};

const stylesInside = StyleSheet.create({
  footerContainer: { flexDirection: 'row', justifyContent: 'space-between' },
});

export default TrustBanner;
