package com.younki.maidmatching.ui;

import static android.content.ContentValues.TAG;

import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.Adapter.MaidAdapter;
import com.younki.maidmatching.services.Adapter.UserData;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.UserResponse;
import com.younki.maidmatching.utils.CircleTransform;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DetailUserFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DetailUserFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "user_id";

    // TODO: Rename and change types of parameters
    private int user_id;

    public DetailUserFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static DetailUserFragment newInstance(int user_id) {
        DetailUserFragment fragment = new DetailUserFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, user_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            user_id = getArguments().getInt(ARG_PARAM1);
        }
    }

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_detail_user, container, false);

        getUser(user_id);

        return rootView;
    }

    private void getUser(int userId) {
        ProgressDialog progressDialog = new ProgressDialog(rootView.getContext());
        progressDialog.setMessage("Loading..."); // Set message untuk dialog
        progressDialog.setCancelable(false); // Set apakah dialog bisa di-cancel atau tidak

        progressDialog.show(); // Menampilkan dialog

        TextView name = rootView.findViewById(R.id.name);
        TextView username = rootView.findViewById(R.id.username);
        TextView email = rootView.findViewById(R.id.email);
        TextView phone = rootView.findViewById(R.id.phone);
        TextView address = rootView.findViewById(R.id.address);
        TextView location = rootView.findViewById(R.id.location);
        TextView birthdate = rootView.findViewById(R.id.birthdate);
        TextView gender = rootView.findViewById(R.id.gender);
        TextView marital_status = rootView.findViewById(R.id.marital_status);
        TextView last_education = rootView.findViewById(R.id.last_education);
        TextView additional_skills = rootView.findViewById(R.id.additional_skills);
        TextView desired_salary = rootView.findViewById(R.id.desired_salary);
        TextView skills = rootView.findViewById(R.id.skills);
        TextView verified = rootView.findViewById(R.id.verified);

        Call<UserResponse> userResponseCall = ApiClient.getUserService(rootView.getContext()).getUserDetail(String.valueOf(userId));

        userResponseCall.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                progressDialog.dismiss();

                if (response.isSuccessful()){
                    UserResponse userResponse = response.body();

                    name.setText(userResponse.getData().get(0).getName());
                    username.setText(userResponse.getData().get(0).getUsername());
                    email.setText(userResponse.getData().get(0).getEmail());
                    phone.setText(userResponse.getData().get(0).getProfile().getPhone());
                    address.setText(userResponse.getData().get(0).getProfile().getAddress());
                    location.setText(userResponse.getData().get(0).getProfile().getLocation());
                    birthdate.setText(userResponse.getData().get(0).getProfile().getBirthdate());
                    gender.setText(userResponse.getData().get(0).getProfile().getGender());
                    marital_status.setText(userResponse.getData().get(0).getProfile().getMarital_status());
                    last_education.setText(userResponse.getData().get(0).getProfile().getLast_education());
                    additional_skills.setText(userResponse.getData().get(0).getProfile().getAdditional_skills());
                    desired_salary.setText(userResponse.getData().get(0).getProfile().getDesired_salary());

                    if (userResponse.getData().get(0).getProfile().getKyc() == 2) {
                        verified.setVisibility(View.VISIBLE);
                    } else {
                        verified.setVisibility(View.GONE);
                    }
                    
                    String html = "<html><ul>";
                    for (int i = 0; i < userResponse.getData().get(0).getSkill().size(); i++) {
                        html += "<li>"+userResponse.getData().get(0).getSkill().get(i).getName()+"</li>";
                    }
                    html += "</ul></html>";
                    skills.setText(Html.fromHtml(html));

                    Picasso.get().load(ApiClient.getBaseUrl()+userResponse.getData().get(0).getProfile().getImage())
                            .transform(new CircleTransform())
                            .into((ImageView) rootView.findViewById(R.id.profile));
                } else {
                    Toast.makeText(rootView.getContext(), "Gagal Mengambil Data", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                progressDialog.dismiss();

                String errorMessage = t.getMessage();
                Log.e(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}