package com.rthoni.camotion.ui.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
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.CamerasBusiness;
import com.rthoni.camotion.dbo.CameraDbo;
import com.rthoni.camotion.dbo.LocationDbo;

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

    private CameraDbo _camera;

    private ImageView _imageView;

    private int _interval = 500;

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

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

    public CameraDbo getCamera() {
        return _camera;
    }

    public void setCamera(CameraDbo camera) {
        _camera = camera;

        ((TextView)findViewById(R.id.textViewName)).setText(_camera.getName());

        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 CameraView(Context context) {
        super(context);
        init();
    }

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

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

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

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

    public void pause()
    {
        _playing = false;
        findViewById(R.id.imageViewPause).setVisibility(VISIBLE);
    }

    public void play()
    {
        _playing = true;
        findViewById(R.id.imageViewPause).setVisibility(GONE);
        if (_promise == null || _promise.getStatus() != LuPromise.LuPromiseStatus.Running) {
            _play();
        }
    }

    private void _play()
    {
        _promise = CamerasBusiness.getImage(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), _camera)
                .then(new LuPromise.LuConsumer<Bitmap>() {
                    @Override
                    public void execute(Bitmap image) {
                        if (_playing) {
                            _imageView.setImageBitmap(image);
                            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) {
                        pause();
                    }
                });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        pause();
    }
}
