package com.oo.seex.sys_state.widget;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.oo.seex.sys_state.Config;
import com.oo.seex.sys_state.R;
import com.oo.seex.sys_state.StateListener;
import com.oo.seex.sys_state.SystemStateManager;
import com.oo.seex.sys_state.state.bean.BaseState;
import com.oo.seex.sys_state.state.bean.BatteryState;
import com.oo.seex.sys_state.state.bean.BluetoothState;
import com.oo.seex.sys_state.state.bean.WifiState;

/**
 * Created by xucon on 2018/5/3.
 */

public class StateView extends LinearLayout implements StateListener {

	TextClock mSystemTimeTv;

	ImageView mBluetoothImg;

	ImageView mWifiImg;

	ImageView mBatteryImg;

	TextView mBatteryTv;

	private Context mContext;

	public SystemStateManager mSystemStateManager;

	public StateView(Context context, AttributeSet attributeSet){
		super(context,attributeSet);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.state_view, this,true);
		mSystemTimeTv = (TextClock) view.findViewById(R.id.system_time_tv);
		mBluetoothImg = (ImageView) view.findViewById(R.id.bluetooth_img);
		mWifiImg = (ImageView) view.findViewById(R.id.wifi_img);
		mBatteryImg = (ImageView) view.findViewById(R.id.battery_img);
		mBatteryTv = (TextView) view.findViewById(R.id.battery_tv);
	}

	public void registStateListener(){
		try {
			mSystemStateManager = new SystemStateManager(mContext,
					Config.FLAG_BLUETOOTH_STATE | Config.FLAG_BATTERY_STATE | Config.FLAG_WIFI_STATE);
			mSystemStateManager.registStateListener(this);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public void unRegistStateListener(){
		try {
			mSystemStateManager.unRegiststateListener();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void onSysStateChange(int flag, BaseState state) {
		switch (flag) {
			case Config.FLAG_BATTERY_STATE:
				BatteryState batteryState = (BatteryState) state;
				if (mBatteryTv != null)
					mBatteryTv.setText(batteryState.getLevel() + "%");
				if (batteryState.getStatus() == BatteryManager.BATTERY_STATUS_CHARGING) {
					mBatteryImg.setImageResource(R.mipmap.power_1);
				} else {
					if (batteryState.getLevel() < 20) {
						mBatteryImg.setImageResource(R.mipmap.power_4);

					} else if (batteryState.getLevel() < 80) {
						mBatteryImg.setImageResource(R.mipmap.power_3);

					} else {
						mBatteryImg.setImageResource(R.mipmap.power_2);
					}
				}
				break;
			case Config.FLAG_BLUETOOTH_STATE:
				BluetoothState bluetoothState = (BluetoothState) state;
				mBluetoothImg.setVisibility(View.VISIBLE);
				switch (bluetoothState.getState()) {
					case BluetoothAdapter.STATE_ON:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_1);
						break;
					case BluetoothAdapter.STATE_OFF:
						mBluetoothImg.setVisibility(View.GONE);
						break;
					case BluetoothAdapter.STATE_TURNING_OFF:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_2);
						break;
					case BluetoothAdapter.STATE_TURNING_ON:
						mBluetoothImg.setImageResource(R.mipmap.bluetooth_2);
						break;
				}
				break;
			case Config.FLAG_TIME_STATE:
				break;
			case Config.FLAG_WIFI_STATE:
				WifiState wifiState = (WifiState) state;
				mWifiImg.setImageResource(R.mipmap.wifi_4);
				mWifiImg.setVisibility(wifiState.isOpen() ? View.VISIBLE : View.GONE);

				if (wifiState.isOpen()) {
					if (!wifiState.isConnect()) {
						mWifiImg.setImageResource(R.mipmap.wifi_3);
					} else {
						switch (wifiState.getStrength()) {
							case 0:
							case 1:
							case 2:
								mWifiImg.setImageResource(R.mipmap.wifi_2);
								break;
							case 3:
							case 4:
								mWifiImg.setImageResource(R.mipmap.wifi_1);
								break;
						}
					}
				}
				break;
		}

	}
}
