package com.android.launcher3.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.HelpActivity;
import com.android.launcher3.R;

public class KToast {

    private Handler handler = new Handler(Looper.getMainLooper()) {

    };

    private WindowManager windowManager;

    private WindowManager.LayoutParams mWindowParams;
    private View mView;

    private Context context;

    private Toast mToast;

    private DisplayMetrics displayMetrics;


    private TextView tvClose, tvHelp;

    private boolean isShowing = false;

    public KToast(Context context) {
        this.context = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        // 配置一些默认的参数
        mWindowParams = new WindowManager.LayoutParams();
        mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mWindowParams.format = PixelFormat.TRANSLUCENT;
//        mWindowParams.windowAnimations = android.R.style.Animation_Toast;
//        mWindowParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        mWindowParams.packageName = context.getPackageName();
        mWindowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        mWindowParams.verticalMargin = 0.1f;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;


        mView = LayoutInflater.from(context).inflate(R.layout.toast_gen_layout, null);

        mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);

        mView.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mView.getParent() != null) {
                    windowManager.removeView(mView);
                    isShowing = false;
                }
            }
        });
        mView.findViewById(R.id.tv_help).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HelpActivity.class);
//                SystemUtils.getDeviceBrand()+Build.MODEL+Build.VERSION.SDK_INT;
                String url = "https://php-parent-child.dankal.cn/v1/kid/index?name=" + SystemUtils.getDeviceBrand();
                intent.putExtra("url", url);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

    public void setText(String msg) {

        ((TextView) mView.findViewById(R.id.tv_msg)).setText(msg);
    }


    public void show() {

        try {
            if (!isShowing) {
                windowManager.addView(mView, mWindowParams);
                isShowing = true;
            }
        } catch (Exception e) {
            mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, displayMetrics.heightPixels / 10);
            mToast.setView(mView);
            mToast.show();

        }

    }


    public boolean isShowing() {
        return isShowing;
    }

    public void dismiss() {
        if (PermissionUtil.hasPermission(context)) {
            if (mView.getParent() != null) {
                windowManager.removeView(mView);
                isShowing = false;
            }
        } else {
            mToast.cancel();
        }
    }
}
