package com.rthoni.camotion.ui.views;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luticate.auth.dbo.LuFullLoginDbo;
import com.luticate.utils.business.LuPromise;
import com.rthoni.camotion.R;
import com.rthoni.camotion.business.SensorsBusiness;
import com.rthoni.camotion.dbo.LocationDbo;
import com.rthoni.camotion.dbo.SensorDbo;
import com.rthoni.camotion.dbo.SensorValueDbo;

/**
 * Created by robin on 12/4/15.
 */
public class SensorView extends RelativeLayout {
    private LuFullLoginDbo _fullLoginDbo = null;
    private LocationDbo _currentLocation = null;

    private SensorDbo _sensor;

    private TextView _textView;

    private int _interval = 500;

    private boolean _playing = false;
    private LuPromise<SensorValueDbo> _promise = null;

    public void setCamotionDbo(LuFullLoginDbo fullLoginDbo, LocationDbo currentLocation)
    {
        _fullLoginDbo = fullLoginDbo;
        _currentLocation = currentLocation;
    }

    public SensorDbo getSensor() {
        return _sensor;
    }

    public void setSensor(SensorDbo sensor) {
        _sensor = sensor;

        ((TextView)findViewById(R.id.textViewName)).setText(_sensor.getName());
        _textView = (TextView)findViewById(R.id.textViewValue);

        /*findViewById(R.id.imageViewPause).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                togglePlay();
            }
        });

        _imageView = (ImageView) findViewById(R.id.imageView);
        _imageView.setContentDescription(_camera.getDescription());
        _imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                togglePlay();
            }
        });*/
    }

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

    public SensorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SensorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init()
    {
        inflate(getContext(), R.layout.sensor_view, this);
    }

    public void togglePlay()
    {
        if (_playing)
        {
            pause();
        }
        else
        {
            play();
        }
    }

    public void pause()
    {
        _playing = false;
    }

    public void play()
    {
        _playing = true;
        if (_promise == null || _promise.getStatus() != LuPromise.LuPromiseStatus.Running) {
            _play();
        }
    }

    private void _play()
    {
        _promise = SensorsBusiness.getValue(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), _sensor)
                .then(new LuPromise.LuConsumer<SensorValueDbo>() {
                    @Override
                    public void execute(SensorValueDbo value) {
                        if (_playing) {
                            _textView.setText(String.valueOf(value.getValue()));
                            Handler h = new Handler();
                            h.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    _play();
                                }
                            }, _interval);
                        }
                    }
                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
                    @Override
                    public void execute(LuPromise.LuPromiseError data) {

                    }
                });
    }
}
