package fanch.multijeu.findwords.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.util.ArrayList;

import fanch.multijeu.common.core.Saveable;
import fanch.multijeu.common.core.Word;

public class LetterView extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
    public interface LetterViewDriver {
        LetterButton getLetterAt(int x, int y);
        void onPathCompleted(String w);
    }

    public static class Attrs extends Saveable {
        public boolean animate=true;
    }

    protected LetterViewDriver mListener;
    protected ArrayList<LetterButton> m_path = new ArrayList<LetterButton>();
    protected int   m_endPathX;
    protected int   m_endPathY;
    protected Attrs m_attrs=new Attrs();

    public LetterView(Context context) {
        super(context);
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSPARENT);
        getHolder().addCallback(this);
        setOnTouchListener(this);
    }

    public LetterView(Context context, LetterViewDriver d) {
        super(context);
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSPARENT);
        getHolder().addCallback(this);
        setOnTouchListener(this);
        setListener(d);
    }

    public void setAttrs(Attrs a){ m_attrs=a;}
    public Attrs getAttrs(){ return m_attrs;}



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

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        reDraw();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    private void clearPath()
    {
        m_path.clear();
    }

    private String pathToString()
    {
        String s="";
        for(int i=0; i<m_path.size(); i++)
        {
            s+=m_path.get(i).getLetter();
        }
        return s;
    }

    private void addToPath(LetterButton b)
    {
        for(int i=0; i<m_path.size(); i++)
            if(m_path.get(i)==b)
                return;
        m_path.add(b);
    }

    @Override
    public boolean onTouch(View v, MotionEvent me) {

        int x = (int)me.getX();
        int y = (int)me.getY();
        switch (me.getAction())
        {
            case MotionEvent.ACTION_UP: {
                Word s = new Word(pathToString());
                clearPath();
                if(mListener !=null)
                    mListener.onPathCompleted(s.toString());
            }
            break;
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE: {
                    if(mListener!=null)
                    {
                        LetterButton btn =   mListener.getLetterAt(x,y);

                        if(btn!=null) {
                            if(m_attrs.animate) btn.shake();
                            addToPath(btn);
                        }
                    }
                    m_endPathX = x;
                    m_endPathY = y;
                }
                break;

        }
        reDraw();
        return true;
    }



    protected void reDraw()
    {
        Canvas c= getHolder().lockCanvas();
        if(c==null) return;
        c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        Paint p = new Paint();
        p.setColor(Color.WHITE);
        p.setStrokeWidth(10);
        p.setAntiAlias(true);
        if(m_path.size()>0) {

            int l = m_path.size() - 1;
            for (int i = 0; i < l; i++) {
                LetterButton a = m_path.get(i);
                LetterButton b = m_path.get(i + 1);

                c.drawLine(a.getLeft() + a.getWidth() / 2,
                        a.getTop() + a.getWidth() / 2, b.getLeft() + b.getWidth() / 2,
                        b.getTop() + b.getWidth() / 2, p);
            }
            LetterButton last = m_path.get(l);

            c.drawLine(last.getLeft() + last.getWidth() / 2,
                    last.getTop() + last.getWidth() / 2,
                        m_endPathX, m_endPathY, p);
        }
        getHolder().unlockCanvasAndPost(c);
    }
}
