package com.example.petstoremobile.adapters;

import android.graphics.Color;
import android.view.*;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.petstoremobile.R;
import com.example.petstoremobile.api.UserApi;
import com.example.petstoremobile.databinding.ItemEmployeeBinding;
import com.example.petstoremobile.dtos.EmployeeDTO;
import com.example.petstoremobile.utils.GlideUtils;

import java.util.List;

public class EmployeeAdapter extends RecyclerView.Adapter<EmployeeAdapter.EmployeeViewHolder> {

    private List<EmployeeDTO> list;
    private OnEmployeeClickListener listener;
    private String baseUrl;
    private String token;

    public interface OnEmployeeClickListener {
        void onEmployeeClick(int position);
    }

    public EmployeeAdapter(List<EmployeeDTO> list, OnEmployeeClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public static class EmployeeViewHolder extends RecyclerView.ViewHolder {
        private final ItemEmployeeBinding binding;

        public EmployeeViewHolder(@NonNull ItemEmployeeBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public EmployeeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemEmployeeBinding binding = ItemEmployeeBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new EmployeeViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull EmployeeViewHolder holder, int position) {
        EmployeeDTO e = list.get(position);
        ItemEmployeeBinding binding = holder.binding;

        binding.tvEmployeeFullName.setText(e.getFullName() != null ? e.getFullName() : "");
        binding.tvEmployeeUsername.setText("@" + (e.getUsername() != null ? e.getUsername() : ""));
        binding.tvEmployeeEmail.setText(e.getEmail() != null ? e.getEmail() : "");

        // Role badge
        String role = e.getRole() != null ? e.getRole() : "STAFF";
        binding.tvEmployeeRole.setText(role);
        
        if ("ADMIN".equalsIgnoreCase(role)) {
            binding.tvEmployeeRole.setBackgroundColor(Color.parseColor("#1a759f"));
        } else {
            binding.tvEmployeeRole.setBackgroundColor(Color.parseColor("#577590"));
        }
        
        // Status text and color
        boolean active = Boolean.TRUE.equals(e.getActive());
        binding.tvEmployeeStatus.setText(active ? "Active" : "Inactive");
        binding.tvEmployeeStatus.setTextColor(active ? Color.parseColor("#4CAF50") : Color.parseColor("#F44336"));

        // Profile image
        if (baseUrl != null && e.getId() != null) {
            String imageUrl = baseUrl + String.format(UserApi.AVATAR_PATH, e.getId());
            GlideUtils.loadImageWithTokenCircle(holder.itemView.getContext(), binding.ivEmployeeProfile, imageUrl, token, R.drawable.placeholder);
        } else {
            binding.ivEmployeeProfile.setImageResource(R.drawable.placeholder);
        }

        holder.itemView.setOnClickListener(v -> listener.onEmployeeClick(position));
    }

    @Override
    public int getItemCount() { return list.size(); }
}
