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.widget.TextView;

import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 从底部弹出的选择文件的dialog
 */
public class ButtomDialogView extends Dialog {

    private boolean iscancelable;
    private boolean isBackCancelable;
    private View view;
    private Context context;
    private int gravity;
    private OnChoseListener mListener;
    private boolean mIsShow;
    private boolean mIsShowOther;

    private ButtomDialogView(Context context, boolean isCancelable, boolean isBackCancelable, int gravity, boolean isShow, boolean isShowOther) {
        super(context, R.style.Dialog_ChoseFile);
        this.context = context;
        this.gravity = gravity;
        this.iscancelable = isCancelable;
        this.isBackCancelable = isBackCancelable;
        this.mIsShow = isShow;
        this.mIsShowOther = isShowOther;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_photo_file, null);
        setContentView(view);
        view.findViewById(R.id.tv_album).setOnClickListener(v -> choseType(1));
        TextView mTvFile = view.findViewById(R.id.tv_file);
        TextView mTvLoginOut = view.findViewById(R.id.tv_login_out);
        TextView mTvChangPwd = view.findViewById(R.id.tv_change_pwd);
        if (mIsShow) {
            mTvFile.setVisibility(View.VISIBLE);
            mTvLoginOut.setVisibility(View.GONE);
            mTvChangPwd.setVisibility(View.GONE);
            mTvFile.setOnClickListener(v -> choseType(2));
        } else {
            mTvFile.setVisibility(View.GONE);
            mTvLoginOut.setVisibility(View.VISIBLE);
            mTvChangPwd.setVisibility(View.VISIBLE);
            mTvLoginOut.setOnClickListener(v -> choseType(5));
            mTvChangPwd.setOnClickListener(v -> choseType(4));
        }
        if (mIsShowOther) {
            mTvLoginOut.setVisibility(View.VISIBLE);
            mTvChangPwd.setVisibility(View.VISIBLE);
            mTvFile.setVisibility(View.VISIBLE);
        } else {
            mTvFile.setVisibility(View.GONE);
            mTvChangPwd.setVisibility(View.GONE);
            mTvLoginOut.setVisibility(View.GONE);
        }
        view.findViewById(R.id.tv_photo).setOnClickListener(v -> choseType(3));
        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);
        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());
    }

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

    /**
     * 选择
     *
     * @param i
     */
    private void choseType(int i) {
        if (mListener != null) {
            switch (i) {
                case 1:
                    mListener.choseAlbum();
                    break;
                case 2:
                    mListener.choseFile();
                    break;
                case 3:
                    mListener.choseShoot();
                    break;
                case 4:
                    mListener.changePwd();
                    break;
                case 5:
                    mListener.loginOut();
                    break;
            }
        }
        this.dismiss();
    }

    public interface OnChoseListener {
        void choseFile();

        void choseAlbum();

        void choseShoot();

        void loginOut();

        void changePwd();
    }

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

        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 ButtomDialogView build() {
            return new ButtomDialogView(this.mContext, this.isCancelable, this.mIsBackable, this.mGravity, mIsShow, mIsShowOther);
        }
    }
}