// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public class WeekState {
// 
// 	public enum HighlightType {
// 		NONE,
// 		REGULAR_FREE,
// 		FREE,
// 		CHANGED_TARGET_TIME
// 	}
// 	
// 	public static class DayRowState {
// 		public String label = "";
// 		public HighlightType labelHighlighted = HighlightType.NONE;
// 		public String in = "";
// 		public String out = "";
// 		public String worked = "";
// 		public String flexi = "";
// 
// 		public boolean highlighted = false;
// 
// 		@NonNull @Override
// 		public String toString() {
// 			return "values: " + label + ", " + in + ", " + out + ", " + worked + ", " + flexi
// 					+ ", highlighted: " + highlighted;
// 		}
// 	}
// 	
// 	public static class SummaryRowState {
// 		public String label = "";
// 		public String worked = "";
// 		public String flexi = "";
// 
// 		@NonNull @Override
// 		public String toString() {
// 			return "values: " + label + ", " + worked + ", " + flexi;
// 		}
// 	}
// 	
// 	public String topLeftCorner = "";
// 	public final SummaryRowState totals = new SummaryRowState();
// 
// 	private final DayRowState[] dayRowStates = {
// 			new DayRowState(), new DayRowState(), new DayRowState(), new DayRowState(),
// 			new DayRowState(), new DayRowState(), new DayRowState()
// 	};
// 	
// 	public DayRowState getRowForDay(DayOfWeek dayOfWeek) {
// 		return dayRowStates[dayOfWeek.ordinal()];
// 	}
// 
// 	@NonNull @Override
// 	public String toString() {
// 		StringBuilder sb = new StringBuilder();
// 		sb.append(topLeftCorner); sb.append("\n");
// 
// 		for (DayOfWeek day : DayOfWeek.values()) {
// 			sb.append(getRowForDay(day).toString());
// 			sb.append("\n");
// 		}
// 
// 		sb.append(totals); sb.append("\n");
// 
// 		return sb.toString();
// 	}
// }
// 
// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public static class DayRowState {
// 	public String label = "";
// 	public HighlightType labelHighlighted = HighlightType.NONE;
// 	public String in = "";
// 	public String out = "";
// 	public String worked = "";
// 	public String flexi = "";
// 
// 	public boolean highlighted = false;
// 
// 	@NonNull @Override
// 	public String toString() {
// 		return "values: " + label + ", " + in + ", " + out + ", " + worked + ", " + flexi
// 				+ ", highlighted: " + highlighted;
// 	}
// }
// 
// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public static class SummaryRowState {
// 	public String label = "";
// 	public String worked = "";
// 	public String flexi = "";
// 
// 	@NonNull @Override
// 	public String toString() {
// 		return "values: " + label + ", " + worked + ", " + flexi;
// 	}
// }


import org.zephyrsoft.trackworktime.model.WeekState.DayRowState;
import org.zephyrsoft.trackworktime.model.WeekState.SummaryRowState;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
import org.pmw.tinylog.Logger;
import org.threeten.bp.DayOfWeek;
import org.zephyrsoft.trackworktime.R;
import org.zephyrsoft.trackworktime.databinding.WeekTableBinding;
import org.zephyrsoft.trackworktime.model.WeekState;
/*
 * This file is part of TrackWorkTime (TWT).
 *
 * TWT is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License 3.0 as published by
 * the Free Software Foundation.
 *
 * TWT is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License 3.0 for more details.
 *
 * You should have received a copy of the GNU General Public License 3.0
 * along with TWT. If not, see <http://www.gnu.org/licenses/>.
 */
package org.zephyrsoft.trackworktime.weektimes;




public class WeekTimesView extends LinearLayout {
	
	private WeekState weekState;
	
	private TableLayout weekTable = null;
	private WeekTableBinding binding;

	public WeekTimesView(@NonNull Context context) {
		super(context);
		startLayoutLoading();
	}

	private void startLayoutLoading() {
		new AsyncLayoutInflater(getContext()).inflate(R.layout.week_table,this, ((view, resid, parent) -> {
			binding = WeekTableBinding.bind(view);
			parent.addView(binding.getRoot());
			onViewReady();
		}));
	}

	private void onViewReady() {
		weekTable = binding.weekTable;
		
		if(isDataSet()) {
			loadWeekState();
		}
	}

	/** Interface for callback when clicking on day **/
	public interface OnDayClickListener {
		void onClick(View v, DayOfWeek day);
	}
	private OnDayClickListener onDayClickListener;
	public void setOnDayClickListener(OnDayClickListener onDayClickListener) {
		this.onDayClickListener = onDayClickListener;
	}
	

	private boolean isDataSet() {
		return weekState != null;
	}

	public void clearWeekState() {
		setWeekState(new WeekState());
	}

	public void setWeekState(@NonNull WeekState weekState) {
		this.weekState = weekState;
		if(isViewReady()) {
			loadWeekState();
		}
	}

	private boolean isViewReady() {
		return weekTable != null;
	}

	private void loadWeekState() {
		if(!isDataSet()) {
			Logger.warn("Loading weekState when data was not set");
			return;
		}
		
		binding.topLeftCorner.setText(weekState.topLeftCorner);

		for (DayOfWeek day : DayOfWeek.values()) {
// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public class WeekState {
// 
// 	public enum HighlightType {
// 		NONE,
// 		REGULAR_FREE,
// 		FREE,
// 		CHANGED_TARGET_TIME
// 	}
// 	
// 	public static class DayRowState {
// 		public String label = "";
// 		public HighlightType labelHighlighted = HighlightType.NONE;
// 		public String in = "";
// 		public String out = "";
// 		public String worked = "";
// 		public String flexi = "";
// 
// 		public boolean highlighted = false;
// 
// 		@NonNull @Override
// 		public String toString() {
// 			return "values: " + label + ", " + in + ", " + out + ", " + worked + ", " + flexi
// 					+ ", highlighted: " + highlighted;
// 		}
// 	}
// 	
// 	public static class SummaryRowState {
// 		public String label = "";
// 		public String worked = "";
// 		public String flexi = "";
// 
// 		@NonNull @Override
// 		public String toString() {
// 			return "values: " + label + ", " + worked + ", " + flexi;
// 		}
// 	}
// 	
// 	public String topLeftCorner = "";
// 	public final SummaryRowState totals = new SummaryRowState();
// 
// 	private final DayRowState[] dayRowStates = {
// 			new DayRowState(), new DayRowState(), new DayRowState(), new DayRowState(),
// 			new DayRowState(), new DayRowState(), new DayRowState()
// 	};
// 	
// 	public DayRowState getRowForDay(DayOfWeek dayOfWeek) {
// 		return dayRowStates[dayOfWeek.ordinal()];
// 	}
// 
// 	@NonNull @Override
// 	public String toString() {
// 		StringBuilder sb = new StringBuilder();
// 		sb.append(topLeftCorner); sb.append("\n");
// 
// 		for (DayOfWeek day : DayOfWeek.values()) {
// 			sb.append(getRowForDay(day).toString());
// 			sb.append("\n");
// 		}
// 
// 		sb.append(totals); sb.append("\n");
// 
// 		return sb.toString();
// 	}
// }
// 
// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public static class DayRowState {
// 	public String label = "";
// 	public HighlightType labelHighlighted = HighlightType.NONE;
// 	public String in = "";
// 	public String out = "";
// 	public String worked = "";
// 	public String flexi = "";
// 
// 	public boolean highlighted = false;
// 
// 	@NonNull @Override
// 	public String toString() {
// 		return "values: " + label + ", " + in + ", " + out + ", " + worked + ", " + flexi
// 				+ ", highlighted: " + highlighted;
// 	}
// }
// 
// Path: app/src/main/java/org/zephyrsoft/trackworktime/model/WeekState.java
// public static class SummaryRowState {
// 	public String label = "";
// 	public String worked = "";
// 	public String flexi = "";
// 
// 	@NonNull @Override
// 	public String toString() {
// 		return "values: " + label + ", " + worked + ", " + flexi;
// 	}
// }


// Path: app/src/main/java/org/zephyrsoft/trackworktime/weektimes/WeekTimesView.java
import org.zephyrsoft.trackworktime.model.WeekState.DayRowState;
import org.zephyrsoft.trackworktime.model.WeekState.SummaryRowState;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
import org.pmw.tinylog.Logger;
import org.threeten.bp.DayOfWeek;
import org.zephyrsoft.trackworktime.R;
import org.zephyrsoft.trackworktime.databinding.WeekTableBinding;
import org.zephyrsoft.trackworktime.model.WeekState;

/*
 * This file is part of TrackWorkTime (TWT).
 *
 * TWT is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License 3.0 as published by
 * the Free Software Foundation.
 *
 * TWT is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License 3.0 for more details.
 *
 * You should have received a copy of the GNU General Public License 3.0
 * along with TWT. If not, see <http://www.gnu.org/licenses/>.
 */
package org.zephyrsoft.trackworktime.weektimes;




public class WeekTimesView extends LinearLayout {
	
	private WeekState weekState;
	
	private TableLayout weekTable = null;
	private WeekTableBinding binding;

	public WeekTimesView(@NonNull Context context) {
		super(context);
		startLayoutLoading();
	}

	private void startLayoutLoading() {
		new AsyncLayoutInflater(getContext()).inflate(R.layout.week_table,this, ((view, resid, parent) -> {
			binding = WeekTableBinding.bind(view);
			parent.addView(binding.getRoot());
			onViewReady();
		}));
	}

	private void onViewReady() {
		weekTable = binding.weekTable;
		
		if(isDataSet()) {
			loadWeekState();
		}
	}

	/** Interface for callback when clicking on day **/
	public interface OnDayClickListener {
		void onClick(View v, DayOfWeek day);
	}
	private OnDayClickListener onDayClickListener;
	public void setOnDayClickListener(OnDayClickListener onDayClickListener) {
		this.onDayClickListener = onDayClickListener;
	}
	

	private boolean isDataSet() {
		return weekState != null;
	}

	public void clearWeekState() {
		setWeekState(new WeekState());
	}

	public void setWeekState(@NonNull WeekState weekState) {
		this.weekState = weekState;
		if(isViewReady()) {
			loadWeekState();
		}
	}

	private boolean isViewReady() {
		return weekTable != null;
	}

	private void loadWeekState() {
		if(!isDataSet()) {
			Logger.warn("Loading weekState when data was not set");
			return;
		}
		
		binding.topLeftCorner.setText(weekState.topLeftCorner);

		for (DayOfWeek day : DayOfWeek.values()) {
			DayRowState currentWeekRow = weekState.getRowForDay(day);