package com.schooling.anzhen.loading;

import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.widget.RelativeLayout;

import com.schooling.anzhen.R;

/**
 * Created by machenike on 2015/8/7.
 */
public class DialogLoading {


    public static Dialog m_LodingDialog = null;
    public static Context m_context;
    public static int m_count = 0;


    public DialogLoading(Context context) {
        m_context = context;
        m_count = 0;
        m_LodingDialog = null;
    }


    public static void StartLodingDialog(Context context) {

        m_context = context;

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Looper.prepare();
                    loading();
                    Looper.loop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

       /* m_context= AddressManager.APP;
        Log.e("m_count", String.valueOf(m_count));
        try {
            if(m_count!=0){
                return;
            }
            Log.e("m_count", String.valueOf(m_count));
            m_count++;
            if (m_count == 1) {
                m_LodingDialog = new Dialog(m_context, R.style.dialog_style);
                m_LodingDialog.setContentView(R.layout.dialog_loding_all);
                m_LodingDialog.setCanceledOnTouchOutside(true);
                Window win = m_LodingDialog.getWindow();
                win.setGravity(Gravity.CENTER);
                // win.setWindowAnimations(R.style.UpdateTravelAnim); // 设置窗口弹出动画
                win.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                m_LodingDialog.setCanceledOnTouchOutside(false);
                ImageView img_loding = (ImageView) m_LodingDialog.findViewById(R.id.img_loding);

                img_loding.setBackgroundResource(R.drawable.dialog_anim);
                AnimationDrawable animationDrawable = (AnimationDrawable) img_loding.getBackground();
                animationDrawable.start();

                Log.e("m_count",String.valueOf(m_count));
                m_LodingDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/

    }


    public static void loading() {

        //m_context= AddressManager.APP;
        Log.e("m_count", String.valueOf(m_count));
        try {
            if (m_count != 0) {
                return;
            }
            Log.e("m_count", String.valueOf(m_count));
            m_count++;
            if (m_count == 1) {
                m_LodingDialog = new Dialog(m_context, R.style.dialog_style);
                m_LodingDialog.setContentView(R.layout.dialog_loding_all);
                m_LodingDialog.setCanceledOnTouchOutside(true);
                Window win = m_LodingDialog.getWindow();
                win.setGravity(Gravity.CENTER);
                // win.setWindowAnimations(R.style.UpdateTravelAnim); // 设置窗口弹出动画
                win.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                m_LodingDialog.setCanceledOnTouchOutside(false);
//                ImageView img_loding = (ImageView) m_LodingDialog.findViewById(R.id.img_loding);

//                img_loding.setBackgroundResource(R.drawable.dialog_anim);
//                AnimationDrawable animationDrawable = (AnimationDrawable) img_loding.getBackground();
//                animationDrawable.start();

                Log.e("m_count", String.valueOf(m_count));
                if (m_LodingDialog != null) {
                    m_LodingDialog.show();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void StopLongLodingDialog() {
        try {
            if (m_count != 0) {
                m_count = 0;
            } else {
                return;
            }
            if (m_count == 0) {
                if (m_LodingDialog != null) {
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    m_LodingDialog.dismiss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void StopLodingDialog() {
        try {
            if (m_count != 0) {
                m_count = 0;
            } else {
                return;
            }
            if (m_count == 0) {
                if (m_LodingDialog != null) {
                    m_LodingDialog.dismiss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
