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


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.TextView;


import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.FragmentMypageBinding;
import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.views.animation.AnimationActivity;
import org.ntlab.acanthus_client.views.paint.PaintActivity;

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

import static android.os.Build.VERSION_CODES.R;

//-----------------------------------------------------------------
//
public class MyPageFragment extends Fragment {

    private MyPageViewModel mypageViewModel;
    private FragmentMypageBinding binding;

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMypageBinding.inflate(inflater, container, false);
        init();

        View root = binding.getRoot();
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onClickAccept(view);
        onClickAnimation(view);
    }

    //-----------------------------------------------------------------
    //
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    //-----------------------------------------------------------------
    //
    public void onClickAccept(View view) {
        Button buttonIsInvited = view.findViewById(org.ntlab.acanthus_client.R.id.buttonIsInvited);

        buttonIsInvited.setOnClickListener(v -> {
            mypageViewModel.acceptInviteRequest();
        });
    }

    //-----------------------------------------------------------------
    //
    public void onClickAnimation(View view) {
        Button button = view.findViewById(org.ntlab.acanthus_client.R.id.buttonAnimation);

        button.setOnClickListener(v -> {
            transitionAnimationActivity();
        });
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //  init
    private void init() {
        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        mypageViewModel = new ViewModelProvider(this).get(MyPageViewModel.class);
        mypageViewModel.init(acanthus);
        startObserve();

        mypageViewModel.checkInvitedRequest();
        mypageViewModel.getAccountInfoRequest();
    }

    //-----------------------------------------------------------------
    // 変数の値の監視
    private void startObserve() {
        observeInvitedText();
        observeUidText();
    }

    //-----------------------------------------------------------------
    // 招待されているテキストの変更監視
    private void observeInvitedText() {
        final Button buttonIsInvited = binding.buttonIsInvited;
        mypageViewModel.getImmutableIsInvited().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                buttonIsInvited.setText(aBoolean.toString());
                buttonIsInvited.setEnabled(aBoolean);
            }
        });
    }

    //-----------------------------------------------------------------
    // Uidテキストの変更監視
    // memo: デバッグ用
    private void observeUidText() {
        final TextView textUid = binding.textUidNo;
        mypageViewModel.getImmutableUidText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                textUid.setText(s);
            }
        });
    }

    //-----------------------------------------------------------------
    //
    private void transitionAnimationActivity() {
        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        Intent intent = new Intent(acanthus, AnimationActivity.class);
        startActivity(intent);
    }
    //-----------------------------------------------------------------

}