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

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
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.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 评论dialog
 */
public class BottomDialogShareView extends Dialog {

    private View view;
    private Context context;
    private OnChoseListener mListener;
    public ImageView mIvImg;


    private BottomDialogShareView(Context context) {
        super(context, R.style.Dialog_ChoseFile);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_share, null);
        setContentView(view);
        LinearLayout mLlShareQQ = view.findViewById(R.id.ll_share_qq);
        LinearLayout mLlShareWeibo = view.findViewById(R.id.ll_share_weibo);
        LinearLayout mLlShareWeChart = view.findViewById(R.id.ll_share_wechart);
        LinearLayout mLlShareMonent = view.findViewById(R.id.ll_share_wechart_monent);
        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        mLlShareWeChart.setOnClickListener(v -> choseType(1));
        mLlShareWeibo.setOnClickListener(v -> choseType(2));
        mLlShareQQ.setOnClickListener(v -> choseType(3));
        mLlShareMonent.setOnClickListener(v -> choseType(4));
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }


    private void showSoftKeyBoardDialog(EditText editText) {
        if (editText != null) {
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            //请求获得焦点
            editText.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(editText, 0);
        }
    }

    public void addOnChoseListener(OnChoseListener listener) {
        this.mListener = listener;
    }

    /**
     * 选择
     *
     * @param i
     */
    private void choseType(int i) {
        if (mListener != null) {
            switch (i) {
                case 1:
                    mListener.shareWeChart();
                    break;
                case 2:
                    mListener.shareWeibo();
                    break;
                case 3:
                    mListener.shareQQ();
                    break;
                case 4:
                    mListener.shareMoment();
                    break;
            }
        }
        this.dismiss();
    }

    public interface OnChoseListener {
        void shareWeChart();

        void shareWeibo();

        void shareQQ();

        void shareMoment();
    }

    public static class DialogBuilder {
        private Context mContext;
        private boolean isCancelable;
        private boolean mIsBackable;
        private int mGravity = Gravity.BOTTOM;
        private OnChoseListener mListener;
        private boolean mIsShowOther;
        private boolean isCommentImg;

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

        public DialogBuilder setIscancelable(boolean iscancelable) {
            this.isCancelable = iscancelable;
            return this;
        }

        public DialogBuilder setIsBackCancelable(boolean isBackCancelable) {
            this.mIsBackable = isBackCancelable;
            return this;
        }

        public DialogBuilder setShowLocation(int gravity) {
            this.mGravity = gravity;
            return this;
        }

        private boolean mIsShow;

        public DialogBuilder setIsShowFile(boolean isShowFile) {
            mIsShow = isShowFile;
            return this;
        }

        public DialogBuilder setIsShowOther(boolean isShow) {
            mIsShowOther = isShow;
            return this;
        }

        public BottomDialogShareView build() {
            return new BottomDialogShareView(this.mContext);
        }

        public DialogBuilder setIsShowPhoto(String isCommentImg) {
            if ("0".equals(isCommentImg)) {
                this.isCommentImg = false;
            } else {
                this.isCommentImg = true;
            }
            return this;
        }
    }
}