#include "shark_charge.h"
#include "measure_vol.h"
#include "app_rs485_1.h"
#include "shark_xl.h"
#include "app.h"

shark_charger_state_t shark_charger_state;
shark_bool shark_charger_enabled;
shark_bool shark_charger_valid;
shark_bool shark_battery_full;
shark_u32 shark_charge_time;
shark_u16 shark_charge_skip;

static shark_u16 shark_charger_remove;
static shark_u16 shark_charger_insert;

void shark_charge_init(void)
{
	rcu_periph_clock_enable(GPIO_RCU_CHG_DET);
	gpio_init(GPIO_PORT_CHG_DET, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_DET);

	rcu_periph_clock_enable(GPIO_RCU_CHG_EN);
	gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
	gpio_init(GPIO_PORT_CHG_EN, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_EN);
}

shark_bool shark_battery_charge_full(void)
{
	if (sub_bms_info_1.connected) {
		if (sub_bms_info_2.connected) {
			if (sub_bms_info_1.packet_common.m_percent > 99 && sub_bms_info_2.packet_common.m_percent > 99) {
				shark_battery_full = shark_true;
			}

			if (sub_bms_info_1.packet_common.m_percent < 97 || sub_bms_info_2.packet_common.m_percent < 97) {
				shark_battery_full = shark_false;
			}
		} else {
			if (sub_bms_info_1.packet_common.m_percent > 99) {
				shark_battery_full = shark_true;
			}

			if (sub_bms_info_1.packet_common.m_percent < 97) {
				shark_battery_full = shark_false;
			}
		}
	} else if (sub_bms_info_2.connected) {
		if (sub_bms_info_2.packet_common.m_percent > 99) {
			shark_battery_full = shark_true;
		}

		if (sub_bms_info_2.packet_common.m_percent < 97) {
			shark_battery_full = shark_false;
		}
	} else {
		return shark_true;
	}

	return shark_battery_full;
}

void shark_charger_set_poll(void)
{
	shark_charger_state = SHARK_CHG_REMOVE;
	shark_charger_insert = 0;
}

shark_bool shark_charger_is_normal(void)
{
	return shark_charger_enabled && (shark_battery_charge_in1() || shark_battery_charge_in2());
}

shark_bool shark_charger_is_allow(void)
{
	if (shark_battery_charge_full()) {
		shark_charger_set_poll();
		return shark_false;
	}

	if (cb_operate_state == CB_BAT1_BAT2_SERIES || cb_operate_state == CB_BAT_NO) {
		return shark_false;
	}

	if (shark_charger_state != SHARK_CHG_INSERT) {
		return shark_false;
	}

	if (Measure_Vol() > SHARK_CHARGER_VOLTAGE_MAX) {
		return shark_false;
	}

	return shark_true;
}

void shark_charger_set_enable(shark_bool enable)
{
	if (enable && shark_charger_is_allow()) {
		println("charger enable");
		shark_charge_time = shark_get_seconds();
		gpio_bit_set(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
		shark_charger_enabled = shark_true;
	} else {
		println("charger disable");
		gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
		shark_charger_enabled = shark_false;
	}

	shark_charger_valid = shark_false;
}

void shark_charger_disable(void)
{
	if (shark_charge_get_time() > SHARK_CHG_TIME_DETECT || shark_charger_valid) {
		println("charger detect");
		shark_charger_insert = 0;
		shark_charge_skip = 6000;
		shark_charger_state = SHARK_CHG_DETECT;
		shark_charger_set_enable(shark_false);
	}
}

void shark_charge_tick(void)
{
	if (gpio_input_bit_get(GPIO_PORT_CHG_DET, GPIO_PIN_CHG_DET) != RESET) {
		if (shark_charger_remove < SHARK_CHG_REMOVE_MIN) {
			if (shark_charger_insert > 0) {
				println("insert: %d", shark_charger_insert);
				shark_charger_insert = 0;
			}

			shark_charge_skip = 0;
			shark_charger_remove++;

			if (shark_charger_remove == SHARK_CHG_REMOVE_MIN) {
				shark_charger_state = SHARK_CHG_REMOVE;
				shark_battery_full = shark_false;
				println("charger remove");
			}
		}
	} else if (shark_charger_insert < SHARK_CHG_INSERT_MIN) {
		if (shark_charger_remove > 0) {
			println("remove: %d", shark_charger_remove);
			shark_charger_remove = 0;
		}

		if (shark_charge_skip > 0) {
			shark_charge_skip--;
		} else {
			shark_charger_insert++;

			if (shark_charger_insert == SHARK_CHG_INSERT_MIN) {
				if (shark_xl_check()) {
					shark_charger_set_poll();
				} else if (shark_battery_charge_full()) {
					shark_charger_set_poll();
					shark_battery_series_locked = shark_false;
				} else {
					shark_charger_state = SHARK_CHG_INSERT;
					println("charger insert");
				}
			}
		}
	}
}

void shark_charge_poll(void)
{
	if (shark_charger_enabled) {
		switch (cb_operate_state) {
		case CB_BAT1:
			if (shark_battery_charge_in1()) {
				shark_charger_valid = shark_true;
			} else {
				shark_charger_disable();
			}
			break;

		case CB_BAT2:
			if (shark_battery_charge_in2()) {
				shark_charger_valid = shark_true;
			} else {
				shark_charger_disable();
			}
			break;

		case CB_BAT1_BAT2_PARRALLEL:
			if (shark_battery_charge_in1()) {
				if (shark_battery_charge_in2()) {
					shark_charger_valid = shark_true;
				} else if (shark_battery_get_current12() < 0) {
					shark_charger_disable();
				}
			} else if (shark_battery_charge_in2()) {
				if (shark_battery_get_current12() < 0) {
					shark_charger_disable();
				}
			} else {
				shark_charger_disable();
			}
			break;
		}
	} else if (shark_charger_state == SHARK_CHG_INSERT) {
		shark_charger_set_enable(shark_true);
	}
}
