// appbar drawar
import { iconsExistList } from "@/components/MobileUiBuilder/AndroidUI/AndroidConfiguration";
import { StringCaser } from "@/components/WebExportReact/src/utils/helpers";

{
  /* <group android:checkableBehavior="single"> */
}
export default (menu, name) => {
  return `
    <LinearLayout
      android:id="@+id/lytHead"
      android:layout_width="match_parent"
      android:layout_height="@dimen/_39sdp"
      android:orientation="horizontal"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      android:layout_marginTop="0dp"
      android:background="@color/black"
      android:weightSum="3"
    >
    ${buildAppBarItemsLeft(menu)}
  
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">

      <TextView
          android:id="@+id/appCompatTextView2"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="${name}"
          android:gravity="center"
          android:textAlignment="center"
          android:textColor="@color/white" />
    </LinearLayout>

    ${buildAppBarItemsRight(menu)}
    
  </LinearLayout>
          `;
};

const buildAppBarItemsLeft = (items) => {
  if (typeof items === "undefined") return "";
  const content = [];
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (typeof item === "undefined") return "";
    if (item?.direction === "left") {
      content.push(`
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:layout_weight="1"
        >
          <ImageView
            android:id="@+id/${item.fragment + i ?? "home"}"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_25sdp"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/_10sdp"
            android:padding="8dp"
            android:src="${iconsExistList.includes(item.icon) ? "@drawable/" + item.icon : "@android:drawable/" + item.icon}" 
          />
        </LinearLayout>
      `)
    } else {
      content.push(`
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:layout_weight="1"
        >
        </LinearLayout>
      `)
    }
  }
  return content.join("\n");
};

const buildAppBarItemsRight = (items) => {
  if (typeof items === "undefined") return "";
  const content = [];
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (typeof item === "undefined") return "";
    if (item?.direction === "right") {
      content.push(`
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:layoutDirection="rtl"
          android:layout_weight="1"
        >
          <ImageView
            android:id="@+id/${item.fragment + i ?? "home"}"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_25sdp"
            android:layout_gravity="center"
            android:layout_marginRight="@dimen/_10sdp"
            android:src="${iconsExistList.includes(item.icon) ? "@drawable/" + item.icon : "@android:drawable/" + item.icon}"
            android:padding="8dp"
          />
        </LinearLayout>
      `);
    } else {
      content.push(`
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:layoutDirection="rtl"
          android:layout_weight="1"
        >
        </LinearLayout>
      `);
    }
  }
  return content.join("\n");
};
