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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;

public class ProfileFragment extends Fragment {

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

        ImageView closeBtn = view.findViewById(R.id.btn_close_profile);
        ImageView profileImage = view.findViewById(R.id.profile_image);
        TextView nameText = view.findViewById(R.id.profile_name);
        TextView emailText = view.findViewById(R.id.profile_email);
        TextView roleText = view.findViewById(R.id.profile_role);
        Button logoutBtn = view.findViewById(R.id.btn_logout);
        Button updateInfoBtn = view.findViewById(R.id.btn_update_info);
        TextView versionText = view.findViewById(R.id.tv_version_info); // NYTT

        UserManager user = UserManager.getInstance();
        nameText.setText(user.getUserDisplayName());
        emailText.setText(user.getUserEmail());
        roleText.setText("Rolle: " + user.getUserRole());

        // NYTT: Sett versjonstekst
        String versionInfo = "Versjon " + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")";
        versionText.setText(versionInfo);

        if (user.getPhotoUrl() != null) {
            Glide.with(this)
                    .load(user.getPhotoUrl())
                    .apply(RequestOptions.circleCropTransform())
                    .into(profileImage);
        }

        closeBtn.setOnClickListener(v -> {
            Navigation.findNavController(view).navigateUp();
        });

        updateInfoBtn.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putInt("formId", 1);
            Navigation.findNavController(view).navigate(R.id.action_profile_to_form, bundle);
        });

        logoutBtn.setOnClickListener(v -> performLogout());

        return view;
    }

    private void performLogout() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(MainActivity.GOOGLE_WEB_CLIENT_ID)
                .requestEmail()
                .build();
        GoogleSignInClient client = GoogleSignIn.getClient(requireActivity(), gso);

        client.signOut().addOnCompleteListener(task -> {
            UserManager.getInstance().logout();
            RetrofitClient.clearClient();
            NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment);
            navController.navigate(R.id.action_profile_to_login);
            Toast.makeText(getContext(), "Du er nå logget ut", Toast.LENGTH_SHORT).show();
        });
    }
}