package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wasu.cs.model.HomeColumnModel;
import com.wasu.widget.drawfocus.DrawFocus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.AppUtil.OnNetStateListener;
import cn.com.wasu.main.BuildConfig;
import cn.com.wasu.main.ChannelFlavor;
import cn.com.wasu.main.R;

public class BottomBar extends RelativeLayout {

    private final static int MSG_UPDATE_TIME = 0;
    private SimpleDateFormat sFormat;
    private final static String m24 = "HH:mm";

    private TextView timeText;
    private ImageView netStatus;

    //	private LinearLayout rightLayout;
    private Context context;
    private OnNetStateListener onNetStateListener;
    private Timer utTimer;
    public BottomFunctionView userCenterView;
    private View fromFocusView, toFocusView;
    // 画焦点动画
    private DrawFocus mDrawFocus;
    private boolean isExit = false; // 退出标记
    private TextView patchText;

    public BottomBar(Context context) {
        super(context);
        init(context);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * @param @param  context
     * @param context
     * @return void
     * @throws
     * @author 海迪
     * @Title: init
     * @Description: 初始化网络状态，添加监听网络状态，初始化时间，使用Timer修改时间timeText
     */
    private void init(final Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.main_bottom_bar, this);
        timeText = (TextView) findViewById(R.id.time_text);
        netStatus = (ImageView) findViewById(R.id.net_status);
        patchText = (TextView) findViewById(R.id.patch_text);
//		rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
//		mDrawFocus = new DrawFocus(this);
//		mDrawFocus.setFocusHightlightDrawable(R.drawable.home_bottom_focus);
//		mDrawFocus.setFocusMovingDuration(100);
//		mDrawFocus.setDrawFoucsId(R.id.selected);
//		mDrawFocus.isCanScale(false);
//		mDrawFocus.setScaleValue(1.2f, 1.2f);
        setTimeText();
        scheduleUpdateTime();

        setPatchText();

        updateNetStatus(AppUtil.getNetConnStatus(context));

        onNetStateListener = new OnNetStateListener() {

            @Override
            public void onStateChanged(int state) {
                updateNetStatus(state);
            }
        };

        AppUtil.addOnNetStateListener(onNetStateListener);

    }

    /**
     * For Test
     * 进行热更新测试，修改resource文件，热更新。
     */
    private void setPatchText() {
        if (ChannelFlavor.isBenQ()) {
            patchText.setText("BenQ" + getContext().getResources().getString(R.string.patch_text_string));
        } else {
            patchText.setText(BuildConfig.FLAVOR + getContext().getResources().getString(R.string.patch_text_string));
        }
    }

    /**
     * @param @param state
     * @param state
     * @return void
     * @throws
     * @author 海迪
     * @Title: updateNetStatus
     * @Description: 网络状态发送变化时，修改网络标识netStatus
     */
    private void updateNetStatus(int state) {
        int netTagId = R.drawable.net_disconnect;
        boolean showNSTag = true;

        switch (state) {
            case AppUtil.WIFI_CONNECTED:
                netTagId = R.drawable.wifi_connect;
                break;
            case AppUtil.WIFI_DISCONNECTED:
                netTagId = R.drawable.net_disconnect;
                break;
            case AppUtil.ETHERNET_CONNECTED:
                showNSTag = false;
                break;
        }

        if (showNSTag) {
            netStatus.setVisibility(ImageView.VISIBLE);
            netStatus.setImageResource(netTagId);
        } else {
            netStatus.setVisibility(ImageView.GONE);
        }
    }

    /**
     * @param
     * @return void
     * @throws
     * @author 海迪
     * @Title: scheduleUpdateTime
     * @Description: 启动定时器，每隔一分钟修改一次应用时间
     */
    private void scheduleUpdateTime() {
        utTimer = new Timer();

        utTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                if (isExit) {
                    return;
                }
                mHandler.removeCallbacks(this);
                mHandler.sendEmptyMessage(MSG_UPDATE_TIME);
            }
        }, 0, 1000 * 60);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_TIME:
                    setTimeText();
                    break;

                default:
                    break;
            }
        }

        ;
    };

    public void setTimeText() {
        if (sFormat == null) {
            sFormat = new SimpleDateFormat(m24, Locale.CHINA);
        }
        if (timeText != null) {
            timeText.setText(sFormat.format(new Date()));
        }
    }

    /**
     * @author 海迪
     * @Title: initRightBar
     * @Description: 初始化右侧的功能入口区
     * @param @param bottomDataList
     * @return void
     * @throws
     * @param bottomDataList
     */
    public HomeColumnModel userCenterData;


    public void initRightBar(List<HomeColumnModel> bottomDataList) {
        // 初始化右边菜单栏
        for (HomeColumnModel c : bottomDataList) {
            BottomFunctionView function = new BottomFunctionView(context);
            if (c != null) {
                if (!TextUtils.isEmpty(c.getLayout())) {
                    if (c.getLayout().equals("Business_Hall")) {
                        userCenterView = function;

                    }
                }
            }
            function.bindData(c);
            function.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {

                        toFocusView = v;
                        mDrawFocus.startFocusAnimation(fromFocusView, toFocusView);

                    } else {
                        fromFocusView = v;
                        mDrawFocus.endFocusAnimation();
                        mDrawFocus.startResetAnimation(toFocusView);
                        if (hasFocus()) {
                            postInvalidate();
                            return;
                        }

                        fromFocusView = null;
                        postInvalidate();
                    }
                }
            });
//			rightLayout.addView(function);
            // rightLayout.requestFocus();
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if (hasFocus()) {
            if (toFocusView != null) {
                mDrawFocus.drawFocusDynamic(canvas);
                mDrawFocus.drawFocusStatic(canvas);

            }
            return;
        }

        fromFocusView = null;
    }

    /**
     * @param
     * @return void
     * @throws
     * @author 海迪
     * @Title: releaseResources
     * @Description: 释放BottomBar占用资源
     */
    public void releaseResources() {
        AppUtil.removeOnNetStateListener(onNetStateListener);

        isExit = true;

        if (utTimer != null)
            utTimer.cancel();

        utTimer = null;

        timeText = null;
        netStatus = null;

//		if (rightLayout != null)
//			rightLayout.removeAllViews();
//
//		rightLayout = null;
    }

}
