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.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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 PaintToolBar paintToolBar;
    private PaintScreenShot paintScreenShot;
    private ActivityPaintBinding binding;
    private PaintViewModel paintViewModel;

    private boolean buttonView = false;

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

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

        paintScreenShot.onCreate();

        Button button = findViewById(R.id.button_keep);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //bottonタップで表示されてたら非表示にする。
                if(buttonView) {
                    findViewById(R.id.capture).setVisibility(View.INVISIBLE);
                    findViewById(R.id.image).setVisibility(View.INVISIBLE);
                    findViewById(R.id.textView3).setVisibility(View.INVISIBLE);

                    buttonView = false;
                }
                else {
                    findViewById(R.id.capture).setVisibility(View.VISIBLE);
                    findViewById(R.id.image).setVisibility(View.VISIBLE);
                    findViewById(R.id.textView3).setVisibility(View.VISIBLE);

                    buttonView = true;
                }
            }
        });
    }

    //-----------------------------------------------------------------
    @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);
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //  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);

        paintScreenShot = new PaintScreenShot(this);
        paintToolBar = new PaintToolBar(this, paintCanvas);

        findViewById(R.id.capture).setVisibility(View.INVISIBLE);
        findViewById(R.id.image).setVisibility(View.INVISIBLE);
        findViewById(R.id.textView3).setVisibility(View.INVISIBLE);
    }

    //-----------------------------------------------------------------
    //
    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());
    }

    //------------------------------------------------------------------
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        paintScreenShot.onActivityResult(requestCode, resultCode, data);
    }

}