package com.bell.floatlayoutdemo;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.Random;

/**
 * @Info
 * @Auth Bello
 * @Time 18/1/7 上午1:21
 * @Ver
 */

public class MainService extends Service implements View.OnClickListener {

    private static final String TAG = "MainService";

    RelativeLayout toucherLayout;
    WindowManager.LayoutParams params;
    WindowManager windowManager;

    ImageView imageButton1;

    //状态栏高度.
    int statusBarHeight = -1;

    //不与Activity进行绑定.
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.i(TAG,"MainService Created");
        createToucher();
    }

    private void createToucher()
    {
        //赋值WindowManager&LayoutParam.
        params = new WindowManager.LayoutParams();
        windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        //设置type.系统提示型窗口，一般都在应用程序窗口之上.
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //设置效果为背景透明.
        params.format = PixelFormat.RGBA_8888;
        //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        //设置窗口初始停靠位置.
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = 0;
        params.y = 300;

        //设置悬浮窗口长宽数据.
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = 1081;

        LayoutInflater inflater = LayoutInflater.from(getApplication());
        //获取浮动窗口视图所在布局.
        toucherLayout = (RelativeLayout) inflater.inflate(R.layout.float_window,null);
        //添加toucherlayout
        windowManager.addView(toucherLayout,params);

        Log.i(TAG,"toucherlayout-->left:" + toucherLayout.getLeft());
        Log.i(TAG,"toucherlayout-->right:" + toucherLayout.getRight());
        Log.i(TAG,"toucherlayout-->top:" + toucherLayout.getTop());
        Log.i(TAG,"toucherlayout-->bottom:" + toucherLayout.getBottom());

        //主动计算出当前View的宽高信息.
        toucherLayout.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);

        //用于检测状态栏高度.
        int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");
        if (resourceId > 0)
        {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        Log.i(TAG,"状态栏高度为:" + statusBarHeight);

        //浮动窗口按钮.
        imageButton1 = (ImageView) toucherLayout.findViewById(R.id.float_img);

        imageButton1.setOnClickListener(new View.OnClickListener() {
            long[] hints = new long[2];
            @Override
            public void onClick(View v) {
                Log.i(TAG,"点击了");
                System.arraycopy(hints,1,hints,0,hints.length -1);
                hints[hints.length -1] = SystemClock.uptimeMillis();
                if (SystemClock.uptimeMillis() - hints[0] >= 700)
                {
                    Log.i(TAG,"要执行");
                    Toast.makeText(MainService.this,"连续点击两次以退出",Toast.LENGTH_SHORT).show();
                }else
                {
                    Log.i(TAG,"即将关闭");
                    stopSelf();
                }
            }
        });

        toucherLayout.findViewById(R.id.float_300).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_400).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_500).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_600).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_700).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_800).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_900).setOnClickListener(this);
        toucherLayout.findViewById(R.id.float_200).setOnClickListener(this);

        /*imageButton1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                params.x = (int) event.getRawX() - 150;
                params.y = (int) event.getRawY() - 150 - statusBarHeight;
                windowManager.updateViewLayout(toucherLayout,params);
                return false;
            }
        });*/
    }

    @Override
    public void onDestroy()
    {
        if (imageButton1 != null)
        {
            windowManager.removeView(toucherLayout);
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.float_200:
                ShellUtil.execShellCmd(getShellStr(200));
                break;
            case R.id.float_300:
                ShellUtil.execShellCmd(getShellStr(300));
                break;
            case R.id.float_400:
                ShellUtil.execShellCmd(getShellStr(400));
                break;
            case R.id.float_500:
                ShellUtil.execShellCmd(getShellStr(500));
                break;
            case R.id.float_600:
                ShellUtil.execShellCmd(getShellStr(600));
                break;
            case R.id.float_700:
                ShellUtil.execShellCmd(getShellStr(700));
                break;
            case R.id.float_800:
                ShellUtil.execShellCmd(getShellStr(800));
                break;
            case R.id.float_900:
                ShellUtil.execShellCmd(getShellStr(900));
                break;
        }
    }


    public String getShellStr(int m){
        int r1 = getRadomInt();
        int x1 = 700+r1;
        int y1 = 1800+r1;
        int m1 = m+r1;
        return  "input swipe "+x1+" "+y1+" "+x1+" "+y1+" "+m1+"";
    }
    public int getRadomInt(){
        Random random = new Random();
        return random.nextInt(15);
    }
}