package org.ntlab.acanthus_client.views.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Position;
import org.ntlab.acanthus_client.entities.Stroke;
import org.ntlab.acanthus_client.resources.HelloWorldRest;
import org.ntlab.acanthus_client.resources.gallery.StrokesRest;

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

import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

//-----------------------------------------------------------------
// 描画キャンバスの本体
public class PaintCanvas extends View implements Runnable{

    private Path path;
    private Path path2;
    private Paint paint;
    private Paint paint2;
    private PaintViewModel paintViewModel;
    private ScheduledThreadPoolExecutor thread = new ScheduledThreadPoolExecutor(1);
    private int localstrokeNo=0;
    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    public PaintCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        path = new Path(); // 図形描画
        path2 = new Path(); // 図形描画
        paint = new Paint();//筆の種類
        paint2 = new Paint();//筆の種類
        paint.setColor(Color.BLACK);//色の指定
        paint.setStyle(Paint.Style.STROKE);//線をひく
        paint.setStrokeWidth(20);//幅
        paint2.setColor(Color.RED);//色の指定
        paint2.setStyle(Paint.Style.STROKE);//線をひく
        paint2.setStrokeWidth(20);//幅
        thread.scheduleWithFixedDelay(this, 1000L, 2000L, TimeUnit.MILLISECONDS);
    }

    //-----------------------------------------------------------------
    // setter
    public void setPaintViewModel(PaintViewModel paintViewModel) {
        this.paintViewModel = paintViewModel;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
        canvas.drawPath(path2, paint2);
    }

    //-----------------------------------------------------------------
    // 描画操作 (条件分岐：押したとき、動かしたとき、放した時）
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //(3-1)座標を取得（x座標、y座標）
        float x = event.getX();
        float y = event.getY();

        //(3-2)タッチの処理
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("motion", "+++++++++++++++++++++++++++++++++++++++down======================================================");
                onTouched(x, y);
                paintViewModel.addLocalStrokeRequest(x,y);
                //paintViewModel.addPositionLocal(x, y);
                break;

            case MotionEvent.ACTION_MOVE:
                Log.d("motion", "move");
                onTouchedMove(x, y);
                paintViewModel.addPositionRequest(x,y);
                break;

            case MotionEvent.ACTION_UP:
                Log.d("motion", "=====================================up======================================================");
                paintViewModel.addPositionRequestfirst();
                break;
        }


        return true;
    }

    //-----------------------------------------------------------------
    // 描画クリア
    public void clearCanvas() {
        paintViewModel.deleteStrokesRequest();
        path.reset();
        path2.reset();
        invalidate();
    }

    //-----------------------------------------------------------------
    // 押した瞬間の処理
    private void onTouched(float x, float y) {
        //path = new Path();
        path.moveTo(x, y);
        invalidate();
    }

    //-----------------------------------------------------------------
    // 押した後動かした時の処理
    private void onTouchedMove(float x, float y) {
        path.lineTo(x, y);
        invalidate();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // 押した瞬間の処理
    private void onTouched2(float x, float y) {
        //path2 = new Path();
        path2.moveTo(x, y);
        invalidate();
    }

    //-----------------------------------------------------------------
    // 押した後動かした時の処理
    private void onTouchedMove2(float x, float y) {
        path2.lineTo(x, y);
        invalidate();
    }

    //描写するための取得
    public void getCanvas() {
        int cnt = 0;
        path2.reset();
        invalidate();
        for (Stroke s : paintViewModel.getStroke().getValue()) {
            for (Position p : s.getPositions()) {
                if (cnt == 0) {
                    onTouched2(p.getX(), p.getY());
                } else{
                    if(p.getX()==-1){

                    }else {
                        onTouchedMove2(p.getX(), p.getY());
                    }
                }
                cnt++;
            }
            cnt = 0;
        }
        paintViewModel.deleteStroke();
    }

    // 一定間隔でサーバー上の筆跡を取得する（GET）
    @Override
    public void run() {
        getCanvas();
    }
}