package com.tenlionsoft.baselib.core.widget.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;

import androidx.appcompat.widget.SwitchCompat;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 添加汽车使用或所属人员
 */
public class ShareDocDialog extends Dialog {

    private View view;
    private Context context;
    private OnChangeListener mChangeListener;
    private SwitchCompat mSwIsKey;
    private int mIsKey = 0;
    private TextView mTvName;
    private CheckBox mCbEdit;
    private CheckBox mCbDel;
    private CheckBox mCbShare;
    private String mIds;
    private String mNames;
    private LinearLayout mLlAuth;

    private ShareDocDialog(Context context) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_show_share_doc, null);
        setContentView(view);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        mTvName = view.findViewById(R.id.tv_name);

        mCbEdit = view.findViewById(R.id.cb_edit);
        mCbDel = view.findViewById(R.id.cb_del);
        mCbShare = view.findViewById(R.id.cb_share);
        mLlAuth = view.findViewById(R.id.ll_auth);


        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        view.findViewById(R.id.tv_confirm).setOnClickListener(v -> doConfirm());
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.dialog_center_alpha);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.9);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
        mTvName.setOnClickListener(v -> mChangeListener.doSel());
    }

    public void setIsShowAuth(int auth) {
        if (1 == auth) {
            mLlAuth.setVisibility(View.VISIBLE);
        } else {
            mLlAuth.setVisibility(View.GONE);
        }
    }

    public void setNames(String ids, String names) {
        this.mNames = names;
        this.mIds = ids;
        mTvName.setText(names);
    }

    private void doConfirm() {
        if (checkParams()) {
            if (mChangeListener != null) {
                String edit = mCbEdit.isChecked() ? "是" : "";
                String del = mCbDel.isChecked() ? "是" : "";
                String share = mCbShare.isChecked() ? "是" : "";
                mChangeListener.doChange(mIds, "", edit, del, share);
            }
        }
    }


    public void HideSoftKeyBoardDialog(Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        } catch (Exception ex) {
        }
    }

    public void addOnChangeListener(OnChangeListener listener) {
        this.mChangeListener = listener;
    }

    public interface OnChangeListener {
        void doChange(String ids, String add, String edit, String del, String share);

        void doSel();
    }

    /**
     * 校验参数
     */
    private boolean checkParams() {
        if (TextUtils.isEmpty(mIds)) {
            ToastUtils.show("请选择人员");
            return false;
        }
        return true;
    }


    public static class DialogBuilder {
        private Context mContext;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }


        public ShareDocDialog build() {
            return new ShareDocDialog(mContext);
        }
    }
}