# Copyright (c) 2020, JINGROW and Contributors
# License: MIT. See LICENSE

import jingrow


def execute():
	if not jingrow.db.table_exists("List View Setting"):
		return
	if not jingrow.db.exists("PageType", "List View Setting"):
		return

	jingrow.reload_pg("desk", "pagetype", "List View Settings")

	existing_list_view_settings = jingrow.get_all("List View Settings", as_list=True, order_by="modified")
	for list_view_setting in jingrow.get_all(
		"List View Setting",
		fields=["disable_count", "disable_sidebar_stats", "disable_auto_refresh", "name"],
		order_by="modified",
	):
		name = list_view_setting.pop("name")
		if name not in [x[0] for x in existing_list_view_settings]:
			list_view_setting["pagetype"] = "List View Settings"
			list_view_settings = jingrow.get_pg(list_view_setting)
			# setting name here is necessary because autoname is set as prompt
			list_view_settings.name = name
			list_view_settings.insert()

	jingrow.delete_pg("PageType", "List View Setting", force=True)
