package com.yono.messeripos;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButton;
import com.yono.messeripos.adapter.CategoryAdapter;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.CategoryServise;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.MainViewModels;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.response.DataResponse;

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

public class FormCategory extends BottomSheetDialogFragment {
    MainViewModels mainViewModels;
    public EditText etCategoryName;
    public MaterialButton btnAddCategory;
    CategoryModels categoryModelsEdit;
    Boolean updateCategory = false;

    public FormCategory() {
    }

    public interface FormCategoryListener {
        void onAddCategory(CategoryModels categoryModels);

        void onUpdateCategory(CategoryModels categoryModels);
    }

    private FormCategoryListener listener;

    public void setListener(FormCategoryListener listener) {
        this.listener = listener;
    }

    public void EditFormCategory(CategoryModels categoryModels){
            this.updateCategory =  true;
            this.categoryModelsEdit = categoryModels;
    }

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

        etCategoryName = v.findViewById(R.id.etCategoryName);
        btnAddCategory = v.findViewById(R.id.btnAddCategory);
        mainViewModels = new ViewModelProvider(getActivity()).get(MainViewModels.class);

        if (!updateCategory){
            etCategoryName.setText("");
        }else{
            etCategoryName.setText(categoryModelsEdit.getNameCategory());
            btnAddCategory.setText("Update Category");
        }

        btnAddCategory.setOnClickListener(view -> addCategory());

        return v;
    }

    private void addCategory() {
        if (!updateCategory){
            CategoryModels categoryModels = new CategoryModels();
            categoryModels.setNameCategory(etCategoryName.getText().toString());
            listener.onAddCategory(categoryModels);
        }else{
            CategoryModels categoryModels = new CategoryModels();
            categoryModels.setNameCategory(etCategoryName.getText().toString());
            categoryModels.setIdCategory(categoryModelsEdit.getIdCategory());
            listener.onUpdateCategory(categoryModels);
        }
    }
}
