import { BenPascalCase } from "@/components/WebExportReact/src/utils/helpers";
import { StringCaser } from "@/utils/utils";
import {
  findComponentsWithBackNavigationProperty,
  findComponentsWithNavigationProperty,
} from "../utils/navigation";
import { getComponentViewBindingId } from "../utils/helpers";
import {
  generateRecyclerViewFragmentBinding,
  generateRecyclerViewImport,
  getRecyclerViewFields,
} from "../utils/recycler";
import {
  generateWebViewImports,
  generateWebViewInteractions,
} from "../utils/widgets";
import {
  cameraFragmentFunctions,
  generateCameraViewImports,
  generateCameraViewInteractions,
  getCameraFragmentFields,
} from "../utils/camera";
import {
  getUploadActionListeners,
  getUploadHelperFunctions,
  getUploadImports,
} from "../utils/fileUpload";
import {
  generateBottomSheetFragmentImports,
  generateBottomSheetFragmentInteractions,
  generateBottomSheetFragmentTrigger,
} from "../utils/bottomSheet";
import {
  billingFragmentFunctions,
  billingFragmentImports,
  // buildBillingFragmentDeps,
  buildBillingFragmentFields,
  buildBillingFragmentTrigger,
  buildFragmentDeps,
} from "../billing/billingFragmentParts";
import {
  callGetFunction,
  generateFormDetails,
  generateSubmitActionListeners,
  generateSubmitFunction,
  generateSubmitImports,
  getFormFragmentFields,
} from "../utils/form";
import {
  buildGraphImports,
  generateGraphFragmentBinding,
  generateGraphFunction,
} from "../utils/graph";
import {
  buildQrCodeScannerImports,
  generateQrCodeScannerFragmentBinding,
  generateQrCodeScannerFunction,
  getQrCodeScannerFields,
} from "../utils/qrCodeScanner";
import {
  buildColorPickerImports,
  generateColorPickerFragmentBinding,
  getColorPickerFields,
  generateColorPickerFunction,
} from "../utils/colorPicker";
import {
  buildDropDownImports,
  generateDropDownFragmentBinding,
  generateDropDownFunction,
  getDropDownFields,
} from "../utils/dropDown";
import {
  generateSearchFunctions,
  generateSearchViewImports,
  generatesearchViewFragmentTrigger,
} from "../utils/search";
import { getArgumentField, getArgumentfields } from "../utils/argumets";
import { buildCheckboxBinding, checkboxImport } from "../utils/checkBox";
import {
  generateCarouselViewFragmentBinding,
  generateCarouselViewImport,
  getCarouselViewFields,
} from "../utils/carousel";
import {
  generateVideoViewImports,
  generateVideoViewInteractions,
  getVideoRecorderFragmentFields,
  videoRecorderFragmentFunctions,
} from "../utils/recorder";
import {
  generateStripePaymentBinding,
  generateStripePaymentField,
  generateStripePaymentFunctions,
  generateStripePaymentImport,
} from "../utils/stripe";
import { buildLayoutDataSourcefunction } from "../utils/layout";
import {
  generateInAppPurchaseBinding,
  generateInAppPurchaseField,
  generateInAppPurchaseFunctions,
  generateInAppPurchaseImport,
} from "../utils/inAppPurchase";
import { generateToolbarAction } from "../utils/toolbarAction";
import {
  buildChatUiImports,
  generateChatUiFragmentBinding,
  generateChatUiFunction,
  getChatUiFields,
} from "../utils/chatui";
import {
  buildRoomListImports,
  generateRoomListFragmentBinding,
  getRoomListFields,
} from "../utils/roomList";
import {
  buildChatBotUiImports,
  generateChatBotUiFragmentBinding,
  generateChatBotUiFunction,
  getChatBotUiFields,
} from "../utils/chatBotUi";
import { generateDatePickerDialogImports, generateDatePickerDialogInteractions, generateTimePickerDialogImports, generateTimePickerDialogInteractions } from "../utils/dialog";

export default (configuration, fragment) => {
  return `
package ${configuration.packageName}.ui
import java.util.Calendar
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import ${configuration.name}.R
import ${configuration.packageName}.data.local.AppPreferences
import ${configuration.name}.databinding.Fragment${BenPascalCase(
    fragment.name
  )}Binding
import ${configuration.packageName}.extensions.*
import ${configuration.packageName}.network.Status
import ${configuration.packageName}.viewmodels.*
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.navigation.fragment.navArgs
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import androidx.appcompat.app.AppCompatActivity
${generateRecyclerViewImport(fragment, configuration)}
${generateCarouselViewImport(fragment, configuration)}
${generateWebViewImports(fragment)}
${generateDatePickerDialogImports(fragment)}
${generateTimePickerDialogImports(fragment)}
${generateCameraViewImports(fragment)}
${generateVideoViewImports(fragment)}
${generateSearchViewImports(fragment, configuration)}
${getUploadImports(fragment)}
${generateSubmitImports(fragment)}
${generateBottomSheetFragmentImports(fragment, configuration)}
${billingFragmentImports(fragment, configuration)}
${buildGraphImports(fragment)}
${buildQrCodeScannerImports(fragment)}
${buildChatUiImports(fragment, configuration)}
${buildChatBotUiImports(fragment, configuration)}
${buildRoomListImports(fragment, configuration)}
${buildColorPickerImports(fragment, configuration)}
${buildDropDownImports(fragment)}
${checkboxImport(fragment)}
${generateStripePaymentImport(fragment, configuration)}
${generateInAppPurchaseImport(fragment, configuration)}
${generateInAppPurchaseImport(fragment, configuration)}

class ${BenPascalCase(
    fragment?.name
  )}Fragment : Fragment(R.layout.fragment_${StringCaser(fragment?.name, {
    separator: "_",
    casetype: "lowercase",
  })}) ${buildFragmentDeps(fragment, configuration)} {

    private val binding by viewBinding(Fragment${BenPascalCase(
    fragment?.name
  )}Binding::bind)
    private val baasViewModel: BaasViewModel by viewModel()
    private val pref by inject<AppPreferences>()
     var userId = pref.userId
    ${getArgumentfields(fragment, configuration)}
    ${buildBillingFragmentFields(fragment, configuration)}
    ${getCameraFragmentFields(fragment)}
    ${getVideoRecorderFragmentFields(fragment)}
    ${getFormFragmentFields(fragment)}
    ${getQrCodeScannerFields(fragment)}
    ${getChatUiFields(fragment)}
    ${getChatBotUiFields(fragment)}
    ${getRoomListFields(fragment)}
    ${getColorPickerFields(fragment)}
    ${getDropDownFields(fragment)}
    ${getRecyclerViewFields(fragment)}
    ${getCarouselViewFields(fragment)}
    ${generateStripePaymentField(fragment)}
    ${generateInAppPurchaseField(fragment)}


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
       // binding.icBack.setOnClickListener { findNavController().popBackStack() }

       ${buildBackNav(fragment)}
       ${getArgumentField(fragment)}
       ${buildFragmentNav(fragment, configuration.fragment)}
       ${generateRecyclerViewFragmentBinding(fragment, configuration)}
       ${generateCarouselViewFragmentBinding(fragment, configuration)}
       ${generatesearchViewFragmentTrigger(fragment, configuration)}
       ${generateWebViewInteractions(fragment)}
       ${generateDatePickerDialogInteractions(fragment)}
       ${generateTimePickerDialogInteractions(fragment)}
       ${generateBottomSheetFragmentInteractions(fragment)}
       ${generateCameraViewInteractions(fragment)}
       ${generateVideoViewInteractions(fragment)}
       ${getUploadActionListeners(fragment)}
       ${buildBillingFragmentTrigger(fragment, configuration)}
       ${callGetFunction(fragment, configuration)}
       ${generateSubmitActionListeners(fragment, configuration)}
      ${generateGraphFragmentBinding(fragment)}
      ${generateQrCodeScannerFragmentBinding(fragment)}
      ${generateChatUiFragmentBinding(fragment)}
      ${generateChatBotUiFragmentBinding(fragment)}
      ${generateRoomListFragmentBinding(fragment)}
      ${generateColorPickerFragmentBinding(fragment)}
      ${buildCheckboxBinding(fragment)}
      ${generateDropDownFragmentBinding(fragment, configuration)}
      ${generateStripePaymentBinding(fragment, configuration)}
      ${generateInAppPurchaseBinding(fragment, configuration)}

   

  ${configuration?.appBar?.length > 0
      ? ` ${configuration?.appBar
        ?.map(
          (bar, index) =>
            `binding.${bar.fragment+ index}.setOnClickListener{

                          findNavController().navigate(R.id.action_${StringCaser(
              fragment?.name,
              { casetype: "capitalize" }
            )}Fragment_to_${StringCaser(bar?.fragment, {
              casetype: "capitalize",
            })}Fragment)
              
                      }
             `
        )
        .join("\n")}
         
                   
    `
      : ""
    }
    }

    ${cameraFragmentFunctions(fragment)}
    ${videoRecorderFragmentFunctions(fragment)}
    ${getUploadHelperFunctions(fragment)}
    ${generateBottomSheetFragmentTrigger(fragment)}
    ${billingFragmentFunctions(fragment, configuration)}
    ${generateToolbarAction(fragment, configuration)}
    ${generateSearchFunctions(fragment, configuration)}
    ${generateFormDetails(fragment, configuration)}
    ${generateSubmitFunction(fragment, configuration)}
    ${generateGraphFunction(fragment, configuration)}
    ${generateQrCodeScannerFunction(fragment, configuration)}
    ${generateChatUiFunction(fragment, configuration)}
    ${generateChatBotUiFunction(fragment, configuration)}
    ${generateColorPickerFunction(fragment, configuration)}
    ${generateDropDownFunction(fragment, configuration)}
    ${generateStripePaymentFunctions(fragment, configuration)}
    ${generateInAppPurchaseFunctions(fragment, configuration)}
    ${buildLayoutDataSourcefunction(fragment, configuration)}
}
  `;
};

function buildBackNav(fragment) {
  let backElements = findComponentsWithBackNavigationProperty(fragment, []);
  const content = [];
  for (const element of backElements) {
    content.push(
      `binding.${getComponentViewBindingId(
        element
      )}.setOnClickListener { findNavController().popBackStack() }`
    );
  }
  return content.join("\n");
}

export function buildFragmentNav(fragment, fragments) {
  let navElements = findComponentsWithNavigationProperty(fragment);
  const content = [];
  const fragmentCapitalizedName = StringCaser(fragment?.name, {
    casetype: "capitalize",
  });
  for (const element of navElements) {
    if (["sign_up", "login"].includes(element.props.to)) continue; //TODO: handle Auth nav
    if (element.parentName === "RecyclerView") continue;
    let destinationFragment = fragments.find(
      (frag) => frag.id == element.props.to
    );
    const destinationCapitalized = StringCaser(destinationFragment?.name, {
      casetype: "capitalize",
    });

    content.push(`
    binding.${getComponentViewBindingId(element)}.setOnClickListener { 
          findNavController().navigate(R.id.action_${fragmentCapitalizedName}Fragment_to_${destinationCapitalized}Fragment)
        }
    `);
  }
  return content.join("\n");
}
