import { BenPascalCase } from "@/components/WebExportReact/src/utils/helpers";

function findColorPickerComponents(data, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findColorPickerComponents(item.components, result);
    }
    if ("children" in item) {
      findColorPickerComponents(item.children, result);
    }
    if (item.name === "ColorPicker") {
      result.push(item);
    }
  }

  return result;
}

export const buildColorPickerImports = (fragment, configuration) => {
  if (findColorPickerComponents(fragment).length == 0) return "";
  return `
import android.widget.Button;
import android.widget.TextView;
import top.defaults.colorpicker.ColorPickerPopup;

import android.graphics.Color
import top.defaults.colorpicker.ColorPickerPopup.ColorPickerObserver
  `;
};

export const getColorPickerFields = (fragment) => {
  if (findColorPickerComponents(fragment).length == 0) return "";

  return `
  private var mDefaultColor = 0
  `;
};

export const generateColorPickerFragmentBinding = (fragment) => {
  if (findColorPickerComponents(fragment).length == 0) return "";

  return `
        mDefaultColor = Color.BLACK

        binding.pickColorButton.setOnClickListener {


            ColorPickerPopup.Builder(requireContext())
            .initialColor(Color.RED)
            .enableBrightness(true)
            .enableAlpha(true)
            .okTitle("Choose")
            .cancelTitle("Cancel")
            .showIndicator(true)
            .showValue(true)
            .build()
            .show(it, object : ColorPickerObserver() {
                override fun onColorPicked(color: Int) {

                    mDefaultColor = color
                    binding.previewSelectedColor.setBackgroundColor(mDefaultColor)
                }

            })
        }

        binding.setColorButton.setOnClickListener {
            binding.gfgHeading.setTextColor(mDefaultColor)
        }
  `;
};

export const generateColorPickerFunction = (fragment, configuration) => {
  if (findColorPickerComponents(fragment).length == 0) return "";

  return `
   override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }
  `;
};
