package org.ntlab.acanthus_client.views.paint;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.ActivityPaintBinding;

//-----------------------------------------------------------------
// 描画ページ
public class PaintActivity extends AppCompatActivity {

    private PaintCanvas paintCanvas;
    private ActivityPaintBinding binding;
    private PaintViewModel paintViewModel;

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    @Override
    public void onDestroy() {
        super.onDestroy();
        paintViewModel.stop();
    }

    //-----------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initView();

    }

    //-----------------------------------------------------------------
    @Override
    protected void onResume() {
        super.onResume();

        displayAid();
    }

    //-----------------------------------------------------------------
    //　描画全体のクリア
    public void onClickClear(View view) {
        paintCanvas.clearCanvas();
    }

    //-----------------------------------------------------------------
    //
    public void onClickInvite(View view) {
        Integer dummyId = 2;
        paintViewModel.inviteNewUserRequest(dummyId);
    }

    //-----------------------------------------------------------------
    //
    public void onClickAddPage(View view) {
        paintViewModel.addPageRequest();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //  init
    private void init() {
        Acanthus acanthus = (Acanthus) getApplication();
        paintViewModel = new ViewModelProvider(this).get(PaintViewModel.class);
        paintViewModel.init(acanthus);

        startObserve();
    }

    //-----------------------------------------------------------------
    //
    private void initView() {
        binding = ActivityPaintBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        paintCanvas = (PaintCanvas) findViewById(R.id.myCanvas);
        paintCanvas.setPaintViewModel(paintViewModel);


    }

    //-----------------------------------------------------------------
    //
    private void startObserve() {
        paintViewModel.getPageNo().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer i) {
                binding.buttonAddPage.setText(String.valueOf(i));
            }
        });
    }

    //------------------------------------------------------------------
    // デバッグ用の現在編集中のAid表示
    private void displayAid() {
        Acanthus acanthus = (Acanthus) getApplication();
        TextView view = findViewById(R.id.textViewCurAid);
        view.setText("aid:" + acanthus.getAid().toString());
    }

    //------------------------------------------------------------------

    //------------------------------------------------------------------
    // 招待ページへ移動
    private void transitionInviteActivity() {
        Acanthus acanthus = (Acanthus) getApplication();
        Intent intent = new Intent(acanthus, InviteActivity.class);
        startActivity(intent);
    }
}