package org.ntlab.acanthus_client.views.main_menu_ui.create;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.FragmentCreateBinding;
import org.ntlab.acanthus_client.databinding.FragmentEditBinding;
import org.ntlab.acanthus_client.views.main_menu_ui.edit.EditViewModel;
import org.ntlab.acanthus_client.views.paint.PaintActivity;

public class CreateFragment extends Fragment {
    private CreateViewModel createViewModel;
    private FragmentCreateBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        createViewModel =
                new ViewModelProvider(this).get(CreateViewModel.class);

        binding = FragmentCreateBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button appButton = view.findViewById(R.id.Paint_button_create);

        //ダミーユーザー
        Integer uid = 1;
        String token = "abc0";

        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        appButton.setOnClickListener(v -> {
            //Integer uid = acanthus.getPreferenceUid();
            String name = ((EditText)view.findViewById(R.id.AnimationNameCreate)).getText().toString();
            //String token = acanthus.getPreferenceToken();
            createViewModel.startWorkCreate(uid, name, token);
            //Integer aid = createViewModel.startWorkCreate(uid, name, token);
            transitionPaintActivity();
        });

        createViewModel.getAidJson().observe(getViewLifecycleOwner(), list -> {
            acanthus.setAid(createViewModel.getAidJson().getValue().getAid());
        });



    }

    //-----------------------------------------------------------------
    // 画面遷移
    private void transitionPaintActivity() {
        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        Intent intent = new Intent(acanthus, PaintActivity.class);
        startActivity(intent);
    }
}
