import { SmartechBaseProps } from "./smartechBaseProps";

export const repositorySnippet = `repositories {
        maven { url 'https://artifacts.netcore.co.in/artifactory/android' }
      }`;

type LanguageType = 'kotlin' | 'java';
type SDKType = 'smartech' | 'hansel';

export const appBuildGradlePath = './android/app/build.gradle';

export const smartechInitSnippet = {
  importStatementJava: `import com.netcore.android.Smartech;\nimport java.lang.ref.WeakReference;\nimport com.netcore.android.logger.SMTDebugLevel;\nimport com.smartechbasereactnative.SmartechBasePlugin;`,
  importStatementKotlin: `import com.netcore.android.Smartech\nimport java.lang.ref.WeakReference\nimport com.netcore.android.logger.SMTDebugLevel\nimport com.smartechbasereactnative.SmartechBasePlugin`,

  onCreateMethodRegexKotlin: /override\s+fun\s+onCreate\(\)\s*\{[^]*?}/g,
  onCreateMethodRegexJava: /@Override\s+public\s+void\s+onCreate\(\)\s*\{[^]*?}/g,
  newArchConditionRegexKotlin: /if\s*\(\s*BuildConfig\.IS_NEW_ARCHITECTURE_ENABLED\s*\)\s*\{[^]*?\n/g,
  newArchConditionRegexJava: /if\s*\(\s*BuildConfig\.IS_NEW_ARCHITECTURE_ENABLED\s*\)\s*\{\s*[^]*?\n/g,
  newArchCheckRegex: /if\s*\(BuildConfig\.IS_NEW_ARCHITECTURE_ENABLED\)\s*\{([\s\S]*?)\}/,

  superOnCreatePatternKotlin: 'super.onCreate()',
  superOnCreatePatternJava: 'super.onCreate();',

  // Function to conditionally create the import statement
  createImportStatementJava: function (props: SmartechBaseProps) {
    let imports = this.importStatementJava;

    // Conditionally add logging import
    let nudges = props.android.smartechNudges;
    if (nudges.smartechNudgesEnabled && nudges.isLogEnabled) {
      imports += `\nimport io.hansel.hanselsdk.Hansel;\n`;
    }
    return imports;
  },

  createImportStatementKotlin: function (props: SmartechBaseProps) {
    let imports = this.importStatementKotlin;

    // Conditionally add logging import
    let nudges = props.android.smartechNudges;
    if (nudges.smartechNudgesEnabled && nudges.isLogEnabled) {
      imports += `\nimport io.hansel.hanselsdk.Hansel\n`;
    }
    return imports;
  }
}
export const hanselTestDeviceSnippet = (iskotlin: boolean) => {
  return {
    importStatement: iskotlin ? `import io.hansel.hanselsdk.Hansel` : `import io.hansel.hanselsdk.Hansel;`,
    testDeviceCode: iskotlin ? `Hansel.pairTestDevice(intent.dataString)` : `Hansel.pairTestDevice(getIntent().getDataString());`,
    superOnCreate: `super.onCreate`
  }
}

interface TestDeviceSnippet {
  importStatement: string,
  superOnCreateStatement: string,
  methodStatement: string
}

interface LanguageSpecificConfig {
  testDeviceCode: string;
}

interface SDKConfig {
  packageName: string,
  kotlin: LanguageSpecificConfig,
  java: LanguageSpecificConfig,
}

const SDK_CONFIG: Record<SDKType, SDKConfig> = {
  hansel: {
    packageName: 'import io.hansel.hanselsdk.Hansel',
    kotlin: {
      testDeviceCode: `Hansel.pairTestDevice(intent.dataString)`
    },
    java: {
      testDeviceCode: `Hansel.pairTestDevice(getIntent().getDataString());`
    }
  },
  smartech: {
    packageName: `import com.netcore.android.Smartech\nimport android.content.Intent\nimport java.lang.ref.WeakReference`,
    kotlin: {
      testDeviceCode: `
        val isSmartechHandledDeeplink = Smartech.getInstance(WeakReference(this)).isDeepLinkFromSmartech(intent)
        if (!isSmartechHandledDeeplink) {
            //Handle deeplink on app side
        }
      `
    },
    java: {
      testDeviceCode: `
        boolean isSmartechHandledDeeplink = Smartech.getInstance(new WeakReference<>(this)).isDeepLinkFromSmartech(getIntent());
        if (!isSmartechHandledDeeplink) {
            //Handle deeplink on app side
        }
      `
    }
  }
}

const generateTestDeviceSnippet = (sdk: SDKType, isKotlin: boolean): TestDeviceSnippet => {
  const language: LanguageType = isKotlin ? 'kotlin' : 'java';
  const config = SDK_CONFIG[sdk];
  const config_lang = config[language];

  return {
    importStatement: `${config.packageName}${isKotlin ? '' : ';'}`,
    methodStatement: config_lang.testDeviceCode,
    superOnCreateStatement: 'super.onCreate'
  }
}

export const hanselTestDeviceUpdatedSnippet = (isKotlin: boolean): TestDeviceSnippet =>
  generateTestDeviceSnippet('hansel', isKotlin);

export const smartechTestDeviceSnippet = (isKotlin: boolean): TestDeviceSnippet =>
  generateTestDeviceSnippet('smartech', isKotlin);

export const smartechInitSnippetKotlin = (props: SmartechBaseProps) => `
  val smartech = Smartech.getInstance(WeakReference(this))
  val smartechBasePlugin = SmartechBasePlugin.getInstance()
  smartechBasePlugin.init(this)
  ${(props.android.isLogEnabled) ? 'smartech.setDebugLevel(SMTDebugLevel.Level.VERBOSE)' : ''}
  smartech.trackAppInstallUpdateBySmartech()
  ${(props.android.smartechNudges.smartechNudgesEnabled && props.android.smartechNudges.isLogEnabled) ? 'Hansel.enableDebugLogs()' : ''}
`.replace(/\n\s*\n/g, '\n').trim();

export const smartechInitSnippetJava = (props: SmartechBaseProps) => `
    Smartech smartech = Smartech.getInstance(new WeakReference(this));
    SmartechBasePlugin smartechBasePlugin = SmartechBasePlugin.getInstance();
    smartechBasePlugin.init(this);
    ${(props.android.isLogEnabled) ? 'smartech.setDebugLevel(SMTDebugLevel.Level.VERBOSE);' : ''}
    smartech.trackAppInstallUpdateBySmartech();
    ${(props.android.smartechNudges.smartechNudgesEnabled && props.android.smartechNudges.isLogEnabled) ? ' Hansel.enableDebugLogs();' : ''}
    
`.replace(/\n\s*\n/g, '\n').trim();

export const dependency = {
  smartechBase: "com.netcore.android:smartech-sdk",
  smartechNudge: "com.netcore.android:smartech-nudges"
}

// SmartechNude encryption key
export const smartechHanselEncryptionKey = "SMT_USE_ENCRYPTION"

// TODO: Remove if not being used.
export const dependencyRegex = {
  smartechBase: /api\s+"com\.netcore\.android:smartech-sdk"/,
  smartechNudge: /api\s+"com\.netcore\.android:smartech-nudges"/
}

export const AutoFetchLocationKey = "SMT_IS_AUTO_FETCHED_LOCATION"