package com.bokecc.dwlivedemo.popup;

import android.app.Dialog;
import android.content.Context;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.bokecc.dwlivedemo.R;


public class PermissionDialog extends Dialog {

    private TextView policyContent;
    private Button ignoreButton;
    private Button agreeButton;
    private TextView tvPermissionTitle;

    public PermissionDialog(Context context) {
        super(context);
        initView(context);
    }

    public PermissionDialog(Context context, int style) {
        super(context, style);
        initView(context);
    }


    private void initView(Context context){
        View view = View.inflate(context, R.layout.dialog_permission,null);
        policyContent = view.findViewById(R.id.policy_content);
        policyContent.setMovementMethod(LinkMovementMethod.getInstance());
        ignoreButton = view.findViewById(R.id.ignore);
        agreeButton = view.findViewById(R.id.agree);
        tvPermissionTitle = view.findViewById(R.id.tv_permission_title);
        setContentView(view);
    }

    public void showBottom(){
        onCreateToBottom();
        show();
    }

    public PermissionDialog setText(String title,String content){
        policyContent.setText(content);
        tvPermissionTitle.setText(title);
        return this;
    }

    public PermissionDialog setOnClickAgree(View.OnClickListener listener){
        agreeButton.setOnClickListener(listener);
        return this;
    }

    public PermissionDialog setOnClickIgnore(View.OnClickListener listener){
        ignoreButton.setOnClickListener(listener);
        return this;
    }

    /**
     * 从底部弹出一个自适应高，宽占满的对话框，并且居底部
     */
    protected void onCreateToBottom() {
        Window window = this.getWindow();
        window.setWindowAnimations(R.style.cs_bottomMenuAnimStyle);
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        //设置弹窗大小为会屏
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        setCanceledOnTouchOutside(false);
        setCancelable(false);
        //去除阴影
        window.setAttributes(params);
    }

}
