package fanch.multijeu.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import fanch.multijeu.R;

public class ComboBox extends LinearLayout implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    protected ArrayList<String> mData = new ArrayList<String>();
    protected int mSelected=0;

    protected String mText;
    protected Button mLeft;
    protected Button mRight;
    public Spinner mSpinner;

    public ComboBox(Context context) {
        super(context);
        init();
    }

    public ComboBox(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.ComboBox, 0, 0);

        try {
            String text = a.getString(R.styleable.ComboBox_description);
            if(text!=null)
                setText(text);
        } finally {
            a.recycle();
        }
    }


    private void init()
    {
        mText="";

        mLeft=new Button(getContext());
        mLeft.setText("<");
        mLeft.setLayoutParams(new LayoutParams(70,-2,0));

        mRight=new Button(getContext());
        mRight.setText(">");
        mRight.setLayoutParams(new LayoutParams(70,-2,0));

        mSpinner=new Spinner(getContext());
        mSpinner.setLayoutParams(new LayoutParams(-2,-1,1));
        mSpinner.setGravity(Gravity.CENTER);

        addView(mLeft);
        addView(mSpinner);
        addView(mRight);

        mRight.setOnClickListener(this);
        mLeft.setOnClickListener(this);
        mSpinner.setOnItemSelectedListener(this);
    }

    public void setData(ArrayList<String> list)
    {
        if(mText==null || mText.length()==0)
        {
            mData=list;
        }else {
            mData.clear();
            for(int i=0; i<list.size(); i++)
                mData.add(mText+" : "+list.get(i));

        }
        mSelected=0;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
                                                R.layout.spinner_item,
                    mData){

        };

        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
    }

    public void setData(ArrayList<String> list, int d) {
        setData(list);
        mSelected=d;
        mSpinner.setSelection(mSelected, false);
    }


    public void setData(String[] list)
    {
        setData(list, 0);
    }

    public void setData(String[] list, int d) {
        ArrayList l = new ArrayList();
        for(int i=0; i<list.length; i++)
            l.add(list[i]);
        setData(l, d);
    }

    public void setText(String s)
    {
        mText=s;
    }


    public int getSelected() {
        return mSelected;
    }

    public void setSelected(int mSelected) {
        this.mSelected = mSelected;
        if(mSelected<0 || mSelected>=mData.size()) return;
        mSpinner.setSelection(mSelected, true);
    }



    @Override
    public void onClick(View v) {
        if(v==mRight)
        {
            setSelected((mSelected+1)%mData.size());
        } else {
            int x;
            if(mSelected-1<0)
                x=mSelected-1+mData.size();
            else
                x=mSelected-1;
            setSelected(x);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mSelected=position;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
