package com.example.quiz;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.quiz.databinding.FragmentSoldByCourseBinding;
import com.example.quiz.utils.Result;
import com.example.quiz.utils.SharedPrefsHelper;
import com.example.quiz.viewModel.PariViewModel;

import dagger.hilt.android.AndroidEntryPoint;

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

@AndroidEntryPoint
public class SoldByCourse extends Fragment {

    FragmentSoldByCourseBinding binding;

    PariViewModel pariViewModel;

    SharedPrefsHelper prefsHelper;


    public static SoldByCourse newInstance() {
        SoldByCourse fragment = new SoldByCourse();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefsHelper = SharedPrefsHelper.getInstance(getContext());
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentSoldByCourseBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);

        binding.btnCheckBalance.setOnClickListener(v -> {
            if(binding.etRaceNumber.getText().toString().isEmpty()){
                binding.etRaceNumber.setError("Veuillez entrer un numéro de course");
                return;
            }
            pariViewModel.getSoldeByCourse(prefsHelper.get("code"), binding.etRaceNumber.getText().toString()).observe(getViewLifecycleOwner(), new Observer<Result<Double>>() {
                @Override
                public void onChanged(Result<Double> doubleResult) {
                    switch (doubleResult.status){
                        case LOADING:{
                            Toast.makeText(getContext(), "Chargement..", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case ERROR:{
                            Toast.makeText(getContext(), doubleResult.message, Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case SUCCESS:{
                            _showPariDialog(doubleResult.data);
                            break;
                        }
                    }
                }
            });
        });
    }

    void _showPariDialog(double solde){
        new  AlertDialog.Builder(getContext())
                .setTitle("Solde")
                .setMessage("Solde la course "+solde)
                .setPositiveButton("Ok", (dialog, which) -> {
                    binding.etRaceNumber.setText("");
                    dialog.dismiss();
                })
                .show();
    }
}