package com.gaoyuan.weixinrecord.manager;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gaoyuan.weixinrecord.R;


public class DialogManager {

    /**
     * 以下为dialog的初始化控件，包括其中的布局文件
     */

    private Dialog mDialog;

    private boolean mbLongtouch = true;

    private RelativeLayout imgBg;

    private TextView tipsTxt;
    private RelativeLayout imgBg2;

    private TextView tipsTxt2;
    private Context mContext;

    private Button btnStop;

    public DialogManager(Context context) {
        mContext = context;
    }

    /**
     * 回调函数，准备完毕，准备好后，button才会开始显示录音框
     *
     * @author nickming
     */
    public interface DialogStopRecordListener {
        void onStoped();
    }

    public DialogStopRecordListener mListener;

    public void setOnDialogStopRecordListener(DialogStopRecordListener listener) {
        mListener = listener;
    }

    public void showRecordingDialog(boolean long_touch) {
        mDialog = new Dialog(mContext, R.style.Theme_audioDialog);
        mbLongtouch = long_touch;
        // 用layoutinflater来引用布局
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_manager, null);
        mDialog.setContentView(view);
        imgBg = (RelativeLayout) view.findViewById(R.id.dm_rl_bg);
        tipsTxt = (TextView) view.findViewById(R.id.dm_tv_txt);
        imgBg2 = (RelativeLayout) view.findViewById(R.id.dm_rl_bg2);
        tipsTxt2 = (TextView) view.findViewById(R.id.dm_tv_txt2);
        btnStop = (Button) view.findViewById(R.id.dm_btn_stop);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.y = -150; // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);

        if (!long_touch){
            mDialog.setCancelable(false);
            mDialog.setCanceledOnTouchOutside(false);

            btnStop.setVisibility(View.VISIBLE);
            tipsTxt.setVisibility(View.GONE);
            tipsTxt2.setVisibility(View.GONE);

            btnStop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null){
                        mListener.onStoped();
                    }
                }
            });
        }

        mDialog.show();
    }

    /**
     * 设置正在录音时的dialog界面
     */
    public void recording() {
        if (mDialog != null && mDialog.isShowing()) {
            imgBg.setVisibility(View.VISIBLE);
            tipsTxt.setVisibility(View.VISIBLE);
            imgBg2.setVisibility(View.GONE);
            tipsTxt2.setVisibility(View.GONE);

            imgBg.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.yuyin_voice_1));
            tipsTxt.setText(R.string.up_for_cancel);
        }
    }

    /**
     * 取消界面
     */
    public void wantToCancel() {
        if (mDialog != null && mDialog.isShowing()) {
            imgBg.setVisibility(View.GONE);
            tipsTxt.setVisibility(View.GONE);
            imgBg2.setVisibility(View.VISIBLE);
            tipsTxt2.setVisibility(View.VISIBLE);

            imgBg2.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.yuyin_cancel));
            tipsTxt2.setText(R.string.want_to_cancle);
            tipsTxt2.setBackgroundColor(mContext.getResources().getColor(R.color.colorRedBg));

        }

    }

    // 时间过短
    public void tooShort() {
        if (mDialog != null && mDialog.isShowing()) {
            imgBg2.setVisibility(View.VISIBLE);
            tipsTxt2.setVisibility(View.VISIBLE);
            imgBg.setVisibility(View.GONE);
            tipsTxt.setVisibility(View.GONE);
            imgBg2.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.yuyin_gantanhao));
            tipsTxt2.setText(R.string.time_too_short);
        }

    }

    // 隐藏dialog
    public void dimissDialog() {

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }

    }

    public void updateVoiceLevel(int level) {
        if (level > 0 && level < 6) {

        } else {
            level = 5;
        }
        if (mDialog != null && mDialog.isShowing()) {

            //通过level来找到图片的id，也可以用switch来寻址，但是代码可能会比较长
            int resId = mContext.getResources().getIdentifier("yuyin_voice_" + level,
                    "drawable", mContext.getPackageName());
            imgBg.setBackgroundResource(resId);
        }

    }

    public TextView getTipsTxt() {
        return tipsTxt;
    }

    public void setTipsTxt(TextView tipsTxt) {
        this.tipsTxt = tipsTxt;
    }
}
