package com.ez.ertc.meeting.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.utils.ViewUtil;

public class FloatingButtonService extends Service {

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    private View contentView;
    private FloatingButtonBinder floatingButtonBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        int width = ViewUtil.dp2px(getApplicationContext(),56);
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = width;
        layoutParams.height = width;
        layoutParams.x = ViewUtil.dp2px(getApplicationContext(),30);
        layoutParams.y = ViewUtil.dp2px(getApplicationContext(),77);

        showFloatingWindow();
    }

    @Override
    public IBinder onBind(Intent intent) {
        if (floatingButtonBinder == null){
            floatingButtonBinder = new FloatingButtonBinder();
        }
        return floatingButtonBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeFloating();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private void showFloatingWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                contentView = View.inflate(getApplicationContext(), R.layout.ezm_widget_float_button,null);
                windowManager.addView(contentView, layoutParams);
                contentView.setOnTouchListener(new FloatingOnTouchListener());
                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (floatingButtonBinder != null){
                            floatingButtonBinder.getOnCloseListener().onClose();
                        }
                    }
                });
            }
        }
    }

    private void removeFloating(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (windowManager != null && contentView != null){
                windowManager.removeView(contentView);
            }
        }
    }

    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int x,xDown;
        private int y,yDown;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    xDown = x;
                    yDown = y;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;
                    windowManager.updateViewLayout(view, layoutParams);
                    break;
                case MotionEvent.ACTION_UP:
                    int nX = (int) event.getRawX();
                    int nY = (int) event.getRawY();
                    if (Math.abs(nX - xDown) > 10 || Math.abs(nY - yDown) > 10){
                        return true;
                    }
                    break;
                default:
                    break;
            }
            return false;
        }
    }

    public static class FloatingButtonBinder extends Binder {
        private OnCloseListener onCloseListener;

        public OnCloseListener getOnCloseListener() {
            return onCloseListener;
        }

        public void setOnCloseListener(OnCloseListener onCloseListener) {
            this.onCloseListener = onCloseListener;
        }
    }

    public interface OnCloseListener {
        void onClose();
    }
}
