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 com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;

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;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//-----------------------------------------------------------------
// ページ編集画面
public class PageActivity extends AppCompatActivity implements Runnable {
    private Acanthus acanthus;
    private PaintViewModel paintViewModel;
    private ActivityPageBinding binding;
    private ScheduledThreadPoolExecutor thread = new ScheduledThreadPoolExecutor(1);


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

        init();
        initView();
    }

    //-----------------------------------------------------------------
    //
    @Override
    public void onDestroy() {
        super.onDestroy();
        thread.shutdown();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ページのサイズを取得し続ける
    @Override
    public void run() {
        paintViewModel.getPageSizeRequest();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ページを追加する
    public void onClickAddPage(View view) {
        paintViewModel.addPageRequest();
    }

    //-----------------------------------------------------------------
    // ページを削除する
    public void onClickDeletePage(View view) {
        paintViewModel.deletePageRequest();
    }

    //-----------------------------------------------------------------
    // ページを次に進める
    public void onClickToAdvancePage(View view) {
        paintViewModel.toAdvanceEditingPage();
    }

    //-----------------------------------------------------------------
    // ページを前に戻す
    public void onClickToBackPage(View view) {
        paintViewModel.toBackEditingPage();
    }

    //-----------------------------------------------------------------
    // ページをスライドして更新する
    public void slidePage(MotionEvent event) {
        paintViewModel.switchToEditPageRequest(event);
    }

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

        thread.scheduleWithFixedDelay(this, 1000L, 2000L, TimeUnit.MILLISECONDS);
    }

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

    }

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