package fanch.multijeu.common.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import fanch.multijeu.R;
import fanch.multijeu.common.core.AnimationEndListener;
import fanch.multijeu.common.utils.TimeUtils;

public class ChronoView extends View {

    public interface ChronoCallback
    {
        void onEnd();
    }

    protected int mEndTime;
    protected int mDuration=1;
    protected ChronoCallback mListener;
    protected boolean mSignSent=false;
    public ChronoView(Context context) {
        super(context);
    }

    public ChronoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mSignSent=true;
    }

    public void setDuration(int d)
    {
        mDuration=d;
    }

    public void start(){
        mEndTime=((int)(TimeUtils.timeMs()))+mDuration;
        mSignSent=false;
    }

    public void stop()
    {
        mSignSent=true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        final int w = getWidth();
        final int h = getHeight();
        final int min = (w<h)?w:h;
        final int size = (int) (min*0.9);
        final int offset = (w-size)/2;
        int delta = 0;//(min-size)/2;
        final Activity c = (Activity) getContext();
        Paint p = new Paint();
        p.setColor(0xff00afaf);
        p.setAntiAlias(true);
        RectF r = new RectF(delta+offset,delta,offset+delta+size, delta+size);

        if(TimeUtils.timeMs()<mEndTime && !mSignSent)
        {
            final int x = (int) (((mEndTime-TimeUtils.timeMs())*360)/mDuration);
            canvas.drawArc(r, -90, x, true, p);

            Paint p2 = new Paint();
            p2.setTextSize(36);
            p2.setTextAlign(Paint.Align.CENTER);
            p2.setColor(0xff000000);
            p2.setAntiAlias(true);
            canvas.drawText(""+(mEndTime-TimeUtils.timeMs()), w/2+delta, h/2+delta, p2);
        }

        postDelayed(new Runnable() {
            @Override
            public void run() {
                c.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(TimeUtils.timeMs()>=mEndTime && mListener!=null && !mSignSent) {
                            mListener.onEnd();
                            mSignSent=true;
                        }
                        invalidate();
                    }
                });
            }
        }, 100);
    }

    public void setListener(ChronoCallback mListener) {
        this.mListener = mListener;
    }

    public void hide()
    {
        if(getVisibility()==GONE) return;
        setVisibility(GONE);
    }

    public void show()
    {
        if(getVisibility()==VISIBLE || getVisibility()==INVISIBLE) return;
        setVisibility(VISIBLE);
    }
}
