package com.kbs.kbsintranett;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.switchmaterial.SwitchMaterial;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class TaskDetailsBottomSheet extends BottomSheetDialogFragment {

    private TaskItem task;
    private OnTaskChangeListener listener;

    public interface OnTaskChangeListener {
        void onTaskChanged();
        void onTaskDeleted(TaskItem task);
        void onEditRequested(TaskItem task); // NYTT
    }

    public TaskDetailsBottomSheet(TaskItem task, OnTaskChangeListener listener) {
        this.task = task;
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_task_details, container, false);

        TextView title = v.findViewById(R.id.detail_task_title);
        TextView date = v.findViewById(R.id.detail_task_date);
        TextView desc = v.findViewById(R.id.detail_task_desc);
        LinearLayout participantsContainer = v.findViewById(R.id.container_participants_status);
        SwitchMaterial switchNotify = v.findViewById(R.id.switch_notifications);
        LinearLayout ownerActions = v.findViewById(R.id.layout_owner_actions);
        Button btnDelete = v.findViewById(R.id.btn_delete_task);
        Button btnEdit = v.findViewById(R.id.btn_edit_task);

        title.setText(task.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd. MMMM yyyy", Locale.getDefault());
        date.setText("Frist: " + sdf.format(new Date(task.getDueDate())));

        if (task.getDescription() != null && !task.getDescription().isEmpty()) {
            desc.setText(task.getDescription());
            desc.setVisibility(View.VISIBLE);
        }

        participantsContainer.removeAllViews();
        for (Map.Entry<String, Boolean> entry : task.getAssigneeStatus().entrySet()) {
            TextView t = new TextView(getContext());
            String status = entry.getValue() ? "✅ Fullført" : "⏳ Pågår";
            t.setText("• " + entry.getKey() + ": " + status);
            t.setPadding(0, 4, 0, 4);
            participantsContainer.addView(t);
        }

        switchNotify.setChecked(task.isNotificationsEnabled());
        switchNotify.setOnCheckedChangeListener((btn, isChecked) -> {
            task.setNotificationsEnabled(isChecked);
            if (listener != null) listener.onTaskChanged();
        });

        if (task.getCreatedByEmail().equalsIgnoreCase(UserManager.getInstance().getUserEmail())) {
            ownerActions.setVisibility(View.VISIBLE);
            btnDelete.setOnClickListener(view -> {
                if (listener != null) listener.onTaskDeleted(task);
                dismiss();
            });
            btnEdit.setOnClickListener(view -> {
                if (listener != null) listener.onEditRequested(task);
                dismiss();
            });
        }

        return v;
    }
}