/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.view.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.R;
public class CommonProgressDialog extends AlertDialog {

    private Util util = new Util();
    private ProgressBar  mProgress;
    private TextView     tvTitle;
    private TextView     tvBody;
    private TextView     tvBtn1;
    private TextView     tvBtn2;
    private LinearLayout llBtn1;
    private LinearLayout llBtn2;
    //    private TextView    mProgressPercent;

    private Handler      mViewUpdateHandler;
    private int          mMax;
    private CharSequence mTitle;
    private CharSequence mBtn1;
    private CharSequence mBtn2;
    private CharSequence mMessage;
    private boolean      mHasStarted;
    private int          mProgressVal;
    Context mContext;

    private String TAG = "CommonProgressDialog";

    public CommonProgressDialog(Context context) {

        super(context);
        initFormats();
    }

    public CommonProgressDialog(Context context, int theme) {

        super(context, theme);
        initFormats();
    }
    @Override protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom_progress);
        mProgress = (ProgressBar) findViewById(R.id.progress);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvBody = (TextView) findViewById(R.id.tvBody);
        tvBtn1 = (TextView) findViewById(R.id.tvBtn1);
        tvBtn2 = (TextView) findViewById(R.id.tvBtn2);
        llBtn1 = (LinearLayout) findViewById(R.id.llBtn1);
        llBtn2 = (LinearLayout) findViewById(R.id.llBtn2);
        llBtn1.setVisibility(View.GONE);
        llBtn2.setVisibility(View.GONE);

        llBtn2.setOnClickListener(new android.view.View.OnClickListener() {

            @Override public void onClick(View arg0) {

                cancel();
            }
        });
        mViewUpdateHandler = new Handler() {

            @Override public void handleMessage(Message msg) {

                super.handleMessage(msg);
            }
        };
        onProgressChanged();
        if ( !util.null2string(mTitle).equals("") ) {
            setTitle(mTitle);
        }
        if ( !util.null2string(mMessage).equals("") ) {
            setMessage(mMessage);
        }
        if ( !util.null2string(mBtn1).equals("") ) {
            setButton1(mBtn1);
        }
        if ( !util.null2string(mBtn2).equals("") ) {
            setButton2(mBtn2);
        }
        if ( mMax > 0 ) {
            setMax(mMax);
        }
        if ( mProgressVal > 0 ) {
            setProgress(mProgressVal);
        }
    }

    private void initFormats() {

        mTitle = "";
        mMessage = "";
        mBtn1 = "";
        mBtn2 = "";
        mMax = 100;
        mProgressVal = 0;
    }

    private void onProgressChanged() {

        mViewUpdateHandler.sendEmptyMessage(0);
    }

    public void setProgressStyle(int style) {
        //mProgressStyle = style;
    }

    public int getMax() {

        if ( mProgress != null ) {
            return mProgress.getMax();
        }
        return mMax;
    }

    public void setMax(int max) {

        if ( mProgress != null ) {
            mProgress.setMax(max);
            onProgressChanged();
        } else {
            mMax = max;
        }
    }

    public void setIndeterminate(boolean indeterminate) {

        if ( mProgress != null ) {
            mProgress.setIndeterminate(indeterminate);
        }
    }

    public void setProgress(int value) {

        if ( mHasStarted ) {
            mProgress.setProgress(value);
            onProgressChanged();
        } else {
            mProgressVal = value;
        }
    }

    @Override public void setMessage(CharSequence message) {

        if ( tvBody != null ) {
            tvBody.setText(message);
        } else {
            mMessage = message;
        }
    }

    @Override public void setTitle(CharSequence title) {

        if ( tvTitle != null ) {
            tvTitle.setText(title);
        } else {
            mTitle = title;
        }
    }

    public void setButton1(CharSequence button1) {

        if ( tvBtn1 != null ) {
            llBtn1.setVisibility(View.VISIBLE);
            tvBtn1.setText(button1);
        } else {
            mBtn1 = button1;
        }
    }

    public void setButton2(CharSequence button2) {

        if ( tvBtn2 != null ) {
            llBtn2.setVisibility(View.VISIBLE);
            tvBtn2.setText(button2);
        } else {
            mBtn2 = button2;
        }
    }

    @Override protected void onStart() {

        super.onStart();
        mHasStarted = true;
    }

    @Override protected void onStop() {

        super.onStop();
        mHasStarted = false;
    }
}
