package com.android.launcher3.floatingwindow;

import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.launcher3.R;

/**
 * Author: roczheng
 * Date: 2020/7/6
 * Time: 11:51
 * Description:
 */
public class FloatPermissionDetectView extends AbsFloatBase {

    int mStartY;
    int mTouchStartY;
    int mTouchCurrentY;

    private String mTipText;

    public FloatPermissionDetectView(Context context, String tipText) {
        super(context);
        this.mTipText = tipText;
    }

    @Override
    public void create() {
        super.create();

        mViewMode = WRAP_CONTENT_TOUCHABLE;

        mGravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;

        inflate(R.layout.dialog_permission_tip_ui);

        findView(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove();
            }
        });

        mInflate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mTouchStartY = (int) event.getRawY();
                        mStartY = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mTouchCurrentY = (int) event.getRawY();
                        WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mInflate.getLayoutParams();
                        layoutParams.y += mTouchCurrentY - mTouchStartY;
                        mWindowManager.updateViewLayout(mInflate, layoutParams);
                        mTouchStartY = mTouchCurrentY;
                }

                return true;
            }
        });
    }

    @Override
    public synchronized void show() {
        super.show();
        TextView tvContent = findView(R.id.tv_content);
        tvContent.setText(Html.fromHtml(mTipText));
    }

    @Override
    protected void onAddWindowFailed(Exception e) {

    }
}
