package com.secspace.lib.common.tips;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.TextView;

import com.secspace.lib.common.R;

/*
 * Create by Colbert on 2019-05-08
 *
 * Copyright (C) 2019 chuanzhigo@gmail.com.
 */
public class TipHandler extends Handler {

    private static final int SHOW_MESSAGE = 1;
    private static final int HIDE_MESSAGE = 0;
    private static final int TIMEOUT = 60000;
    private Context mContext;
    private WindowManager mWindowManager;
    private TextView mTipView;

    TipHandler(Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == SHOW_MESSAGE) {
            if (mWindowManager == null) {
                mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            }
            if (mTipView == null) {
                mTipView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.common_activity_tip, null);
                WindowManager.LayoutParams p = new WindowManager.LayoutParams();
                p.width = WindowManager.LayoutParams.MATCH_PARENT;
                p.height = WindowManager.LayoutParams.WRAP_CONTENT;
                p.gravity = Gravity.BOTTOM;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    p.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                } else {
                    p.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
                }
                p.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                p.format = 1;
                mWindowManager.addView(mTipView, p);
            }
            mTipView.setText(String.valueOf(msg.obj));
            sendEmptyMessageDelayed(HIDE_MESSAGE, msg.arg1);
        } else {
            try {
                if (mTipView != null && mWindowManager != null) {
                    mWindowManager.removeViewImmediate(mTipView);
                    mTipView = null;
                    mWindowManager = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                removeMessages(TIMEOUT);
            }
        }
    }

    public boolean showTip(Context context, String message) {
        return showTip(context, message, TIMEOUT);
    }

    public boolean showTip(Context context, String message, int timeout) {
        mContext = context.getApplicationContext();
        removeMessages(HIDE_MESSAGE);
        Message msg = obtainMessage(SHOW_MESSAGE);
        msg.obj = message;
        msg.arg1 = timeout;
        return sendMessage(msg);
    }

    public void hideTip() {
        sendEmptyMessage(HIDE_MESSAGE);
    }
    public void hideTip(long delaymillis) {
        sendEmptyMessageDelayed(HIDE_MESSAGE,delaymillis);
    }
}
