<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="item"
            type="com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem" />

        <variable
            name="viewModel"
            type="com.topjohnwu.magisk.ui.superuser.SuperuserViewModel" />

    </data>

    <com.google.android.material.card.MaterialCardView
        style="@style/Widget.Card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:foreground="?attr/selectableItemBackground"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:onClick="@{() -> item.toggle()}"
        app:cardCornerRadius="@dimen/radius_generic"
        app:cardElevation="2dp"
        app:cardPreventCornerOverlap="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="@dimen/margin_generic_half">

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/app_icon"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:gravity="end"
                    android:src="@{item.icon}"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/app_name"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:srcCompat="@drawable/ic_logo" />

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/app_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/margin_generic"
                    android:layout_marginRight="@dimen/margin_generic"
                    android:ellipsize="end"
                    android:maxLines="1"
                    android:text="@{item.item.appName}"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textIsSelectable="false"
                    app:layout_constraintBottom_toTopOf="@+id/package_name"
                    app:layout_constraintEnd_toStartOf="@+id/master_switch"
                    app:layout_constraintStart_toEndOf="@+id/app_icon"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_chainStyle="packed"
                    tools:text="@string/app_name" />

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/package_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:maxLines="1"
                    android:text="@{item.item.packageName}"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@android:color/tertiary_text_dark"
                    android:textIsSelectable="false"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="@id/app_name"
                    app:layout_constraintStart_toStartOf="@id/app_name"
                    app:layout_constraintTop_toBottomOf="@id/app_name"
                    tools:text="com.topjohnwu.magisk" />

                <androidx.appcompat.widget.SwitchCompat
                    android:id="@+id/master_switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:checked="@={item.isEnabled}"
                    android:gravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@id/app_name"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/expand_layout"
                gone="@{!item.isExpanded}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="@dimen/margin_generic_half"
                android:paddingTop="@dimen/margin_generic_half"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/package_name">

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/bell"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="16dp"
                    android:tint="@color/icon_grey"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/notification_switch"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_notifications" />

                <androidx.appcompat.widget.SwitchCompat
                    android:id="@+id/notification_switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:checked="@={item.shouldNotify}"
                    android:gravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/guideline"
                    app:layout_constraintStart_toEndOf="@+id/bell"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.4" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/bug"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:tint="@color/icon_grey"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/logging_switch"
                    app:layout_constraintStart_toStartOf="@+id/guideline"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_bug_report" />

                <androidx.appcompat.widget.SwitchCompat
                    android:id="@+id/logging_switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="16dp"
                    android:checked="@={item.shouldLog}"
                    android:gravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/guideline2"
                    app:layout_constraintStart_toEndOf="@+id/bug"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.8" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/more_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:tint="@color/icon_grey"
                    android:visibility="gone"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/delete"
                    app:layout_constraintStart_toStartOf="@id/guideline2"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_more" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/delete"
                    style="@style/Widget.Icon"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:onClick="@{() -> viewModel.deletePressed(item)}"
                    android:tint="@color/icon_grey"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@id/more_info"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_delete" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

</layout>