import React from 'react';
import Shakebug, { AddEvent } from '../src/Screens/Shakebug';
import { NavigationContainer, findFocusedRoute } from '@react-navigation/native';
import { getLocales } from '../GetLocals/src';
import { NativeModules, Text, View, findNodeHandle } from 'react-native';
import { useGlobalState } from './Componanats/GlobalStateContext'

let Languages = ['ENGLISH', 'HINDI', 'GUJARATI', 'ARABIC', 'CHINESE', 'DUTCH', 'FRENCH', 'GERMAN', 'GREEK', 'INDONESIAN', 'ITALIAN', 'JAPANESE', 'KOREAN', 'RUSSIAN', 'SPANISH', 'TURKISH', 'UKRAINIAN', 'MARATHI', 'TAMIL', 'TELUGU']
let LanguageCodes = ['en', 'hi', 'gu', 'ar', 'zh', 'nl', 'fr', 'de', 'el', 'id', 'it', 'ja', 'ko', 'ru', 'es', 'tr', 'uk', 'mr', 'ta', 'te']
let isLocalLanguage = getLocales()[0].languageCode;
let LocalLanguage = LanguageCodes.indexOf(getLocales()[0].languageCode)


// Define the props interface for ShakebugView
interface ShakebugViewProps {
  children?: React.ReactNode;
  Android_appkey?: string;
  iOS_appkey?: string;
  showTutorialScreenFirstTime?: boolean;
  ShakebugThemeColor?: string;
  changeSDKScreenTitle?: string[];
  allowToReportBugByShakingMobile?: boolean;
  allowToReportBugByScreenCapture?: boolean;
  allowCrashReport: boolean;
  makeLogEnabled: boolean;
  ShakebugLanguage: 'ENGLISH' | 'HINDI' | 'GUJARATI' | 'ARABIC' | 'CHINESE' | 'DUTCH' | 'FRENCH' | 'GERMAN' | 'GREEK' | 'INDONESIAN' | 'ITALIAN' | 'JAPANESE' | 'KOREAN' | 'RUSSIAN' | 'SPANISH' | 'TURKISH' | 'UKRAINIAN' | 'MARATHI' | 'TAMIL' | 'TELUGU';
  setShakebugSDKEndPointURL: string[];
}


// ShakebugView component
const ShakebugView: React.FC<ShakebugViewProps> = ({
  children,
  Android_appkey,
  iOS_appkey,
  showTutorialScreenFirstTime = true,
  ShakebugThemeColor = '#1dd3e3',
  changeSDKScreenTitle = ['Annotate your Bug', 'Add Details'],
  allowToReportBugByShakingMobile = true,
  allowToReportBugByScreenCapture = false,
  allowCrashReport = true,
  makeLogEnabled = false,
  ShakebugLanguage = LocalLanguage != -1 ? Languages[LocalLanguage] : "ENGLISH",
  setShakebugSDKEndPointURL = ["https://www.shakebug.com/api/v1.3/webservice.php/", "MarchShake2022", ""]
}) => {

  return (
    <NavigationContainer>
      <Shakebug
        children={children}
        Android_appkey={Android_appkey}
        iOS_appkey={iOS_appkey}
        showTutorialScreenFirstTime={showTutorialScreenFirstTime}
        ShakebugThemeColor={ShakebugThemeColor}
        changeSDKScreenTitle={changeSDKScreenTitle}
        allowToReportBugByScreenCapture={allowToReportBugByScreenCapture}
        allowToReportBugByShakingMobile={allowToReportBugByShakingMobile}
        allowCrashReport={allowCrashReport}
        makeLogEnabled={makeLogEnabled}
        ShakebugLanguage={Languages.includes(ShakebugLanguage) ? ShakebugLanguage : LocalLanguage != -1 ? Languages[LocalLanguage] : "ENGLISH"}
        setShakebugSDKEndPointURL={setShakebugSDKEndPointURL}
      />
    </NavigationContainer>
  );
};

export default ShakebugView;

export const addEventKey = (name: string, value: any): void => {
  AddEvent(name, value)
};

export const triggerBugReporting = (): void => {

  global.isShakebugOpen = false;
  global.isTrigger = true;
}


export const ShakebugSdkProtectedView = ( {children}) => {

  const { isRecordingStart, isScreenShot } = useGlobalState();
  if (isRecordingStart == true || isScreenShot == true) {
    return (
      <View
        style={{
          backgroundColor: 'black',
          opacity: 0.8
        }}
      >
        <View
          style={{
            opacity: 0
          }}
        >
          {children}
        </View>
      </View>

    )
  }
  else {
    return (
      <View
        style={{
          opacity: 1
        }}
      >
        {children}
      </View>
    )
  }
}

export const IsShakebugSDKOpen = () => {
  return global.isShakebugSDKOpen;
}
