package com.stm.asset.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.stm.asset.R;
import com.stm.asset.utils.ViewUtil;

import java.util.Timer;
import java.util.TimerTask;

public class LoadingDialog extends Dialog implements OnDismissListener{

	public static final int SUCCESS =1;
	public static final int FAILURE =2;
	public static final int WARN =3;
	private Activity activity;
	private View view;
    private TextView msg_txt;
    private ImageView logoImg;
    private ProgressBar probar;
    private LinearLayout bgLayout;
    private int width;
    private String message = null;
    private LayoutParams params;
    private OnLoadingDialogResultListener listener;
    private int post_time =0;
    private int tag =0;
    private int state;

    public interface OnLoadingDialogResultListener{
    	void dialogResult(int tag, int state);
    }

    public LoadingDialog(Activity context) {
        super(context,R.style.LoadDialog);
        this.activity =context;
        message = "正在加载...";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(this.activity).inflate(R.layout.view_common_loading_dialog, null);
        msg_txt = view.findViewById(R.id.common_loading_msg);
        probar = view.findViewById(R.id.common_loading_probar);
        logoImg = view.findViewById(R.id.common_loading_logo_img);
        bgLayout = view.findViewById(R.id.common_loading_bg_layout);
        msg_txt.setText(this.message);
        int width = (int) (ViewUtil.getScreenWidth(activity) / 5.0 *3  / 4.0);
        ViewUtil.measureView(msg_txt);
        FrameLayout.LayoutParams param =new FrameLayout.LayoutParams(width, width);
        probar.setLayoutParams(param);
        logoImg.setLayoutParams(param);

        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        this.setContentView(view);
        this.setOnDismissListener(this);
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
    }

    public void setText(String message) {
        this.message = message;
        msg_txt.setText(this.message);
    }

    public void setText(int resId) {
        setText(getContext().getResources().getString(resId));
    }

    public void setWidth(int width){
    	this.width =width;
    }

    public void setOnLoadingDialogResultListener(OnLoadingDialogResultListener listener){
    	this.listener =listener;
    }

    public void setTag(int tag){
    	this.tag =tag;
    }

    @Override
	public void show() {
		super.show();
		probar.setVisibility(View.VISIBLE);
    	msg_txt.setText(message);
	}

    private void closeDialog(){
    	final Timer t = new Timer();
    	LoadingDialog.this.dismiss();
        t.schedule(new TimerTask() {
            public void run() {
                if (listener!=null){
                    listener.dialogResult(tag, state);
                }
                t.cancel();
            }
        }, post_time);
    }

	@Override
	public void onDismiss(DialogInterface dialog) {
		state =0;
	}

	OnKeyListener keylistener = new OnKeyListener(){
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0) {
             return true;
            } else {
             return false;
            }
        }
    };
}
