package org.ntlab.acanthus_client.views.paint.page;

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

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.ActivityPageBinding;
import org.ntlab.acanthus_client.views.paint.PaintViewModel;

//-----------------------------------------------------------------
// ページ編集画面
public class PageActivity extends AppCompatActivity {
    private Acanthus acanthus;
    private PaintViewModel paintViewModel;
    private ActivityPageBinding binding;

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);

        init();
        initView();
    }


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

    //-----------------------------------------------------------------
    //
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();

        //-----------------------------------------------------------------
        switch (action) {

            case MotionEvent.ACTION_MOVE:
                slidePage(event);

                break;
        }

        return true;
    }

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

        startObserve();
    }

    //-----------------------------------------------------------------
    //
    public void slidePage(MotionEvent event) {
        paintViewModel.updatePageNo(event);
    }

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

    }

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

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