package com.example.quiz;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

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

import com.example.quiz.databinding.FragmentSoldBinding;
import com.example.quiz.utils.Result;
import com.example.quiz.utils.SharedPrefsHelper;
import com.example.quiz.viewModel.PariViewModel;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.Calendar;

import dagger.hilt.android.AndroidEntryPoint;

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

    FragmentSoldBinding binding;

    PariViewModel pariViewModel;
    SharedPrefsHelper prefsHelper;

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

        // TODO: Rename and change types and number of parameters
    public static Sold newInstance() {
        Sold fragment = new Sold();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefsHelper = SharedPrefsHelper.getInstance(getContext());
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if(activity != null){
            MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
            activity.setSupportActionBar(toolbar);
            if(activity.getSupportActionBar() != null){
                activity.getSupportActionBar().setTitle("Soldes");
            }
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentSoldBinding.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.btnByCourse.setOnClickListener(v -> {
            FragmentManager fragmentManager = getParentFragmentManager();
            SoldByCourse soldByCourse = SoldByCourse.newInstance();
            fragmentManager.beginTransaction()
                    .replace(R.id.nav_host_fragment_content_main, soldByCourse)
                    .addToBackStack(null)
                    .commit();
        });

        binding.btnByDay.setOnClickListener(v->{
            _showCalendar();
        });
    }

    void _showCalendar(){
        Calendar calendar = Calendar.getInstance();

        DatePickerDialog datePickerDialog = new DatePickerDialog(
                getContext(),
                (view, year, month, dayOfMonth) -> {
                    String date = year + "-" + (month + 1) + "-" + dayOfMonth;
                    pariViewModel.getSoldeByDay(prefsHelper.get("code"), date).observe(getViewLifecycleOwner(), new Observer<Result<Double>>() {
                        @Override
                        public void onChanged(Result<Double> doubleResult) {
                          switch (doubleResult.status){
                              case LOADING:{
                                  Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
                                  break;
                              }
                              case ERROR:{
                                  Log.d("Response", doubleResult.message);
                                  break;
                              }
                              case SUCCESS:{
                                  _showSold(doubleResult.data);
                                  break;
                              }
                          }
                        }
                    });
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );

        datePickerDialog.show();
    }

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