package org.ntlab.acanthus_client.views.animation;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.views.animation_detail.AnimationDetailActivity;
import org.ntlab.acanthus_client.views.paint.PaintCanvas;
import org.ntlab.acanthus_client.views.paint.PaintToolBar;
import org.ntlab.acanthus_client.views.paint.PaintViewModel;

// 表示ページ
public class AnimationActivity extends AppCompatActivity {

    private AnimationCanvas animationCanvas;
    private AnimationViewModel animationViewModel;
    private View view;

    private WebView webView;
    private String url;

    //ボタン系
    private boolean buttonView = true;
    private Button buttonPlayback;
    private Button buttonReset;
    private Button buttonClose;
    private Button buttonDetail;
    private Button buttonSkip;
    private Button buttonBack;


    //animationCanvasの表示
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        init();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        animationCanvas = this.findViewById(R.id.animationMyCanvas);

        //wecView
        webView = this.findViewById(R.id.animationWebview);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
//        url = animationViewModel.getAnimationPage().getValue();
//        //url = "http://nitta-lab-www.is.konan-u.ac.jp/gallery/300/0/a300p0.png";
//        webView.loadUrl(url);

        //アニメーションの再生停止
        buttonPlayback = findViewById(R.id.button_playback);
        buttonPlayback.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                if(animationCanvas.onClickPlayback() == 0){
                    buttonPlayback.setText("start");
                }
                else {
                    buttonPlayback.setText("stop");
                }
            }
        });

        //最初に戻す
        buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                animationCanvas.resetAnimationPage();
            }
        });

        //2秒スキップ
        buttonSkip = findViewById(R.id.button_skip);
        buttonSkip.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                animationCanvas.setAnimationSkip(1);
            }
        });

        //2秒巻き戻し
        buttonBack = findViewById(R.id.button_back);
        buttonBack.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                animationCanvas.setAnimationSkip(-1);
            }
        });


        //閉じる
        buttonClose = findViewById(R.id.button_close);
        buttonClose.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                finish();
            }
        });

        //詳細画面へ移動
        buttonDetail = findViewById(R.id.button_detail);
        buttonDetail.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Intent intent = new Intent(AnimationActivity.this, AnimationDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    public void init(){
        //初期化
        Acanthus acanthus = (Acanthus) getApplication();
        animationViewModel = new ViewModelProvider(this).get(AnimationViewModel.class);
        animationViewModel.init(acanthus);
        startObserver();
    }
    //startobserve
    private void startObserver(){
        animationViewModel.getAnimationPage().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                url = animationViewModel.getAnimationPage().getValue();
                //url = "http://nitta-lab-www.is.konan-u.ac.jp/gallery/300/0/a300p0.png";
                webView.loadUrl(url);
            }
        });
    }


    //Activity内のボタンの表示/非表示
    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                //画面タップで表示されてたら非表示にする。
                if(buttonView) {
                    buttonReset.setVisibility(View.INVISIBLE);
                    buttonPlayback.setVisibility(View.INVISIBLE);
                    buttonDetail.setVisibility(View.INVISIBLE);
                    buttonClose.setVisibility(View.INVISIBLE);
                    buttonSkip.setVisibility(View.INVISIBLE);
                    buttonBack.setVisibility(View.INVISIBLE);

                    buttonView = false;
                }
                else {
                    buttonReset.setVisibility(View.VISIBLE);
                    buttonPlayback.setVisibility(View.VISIBLE);
                    buttonDetail.setVisibility(View.VISIBLE);
                    buttonClose.setVisibility(View.VISIBLE);
                    buttonSkip.setVisibility(View.VISIBLE);
                    buttonBack.setVisibility(View.VISIBLE);

                    buttonView = true;
                }
        }
        return false;
    }

}