import Vue from 'vue'
import Vuex from 'vuex'

import thread from './modules/thread'
import category from './modules/category'
import moderation from './modules/moderation'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		meta: {
			name: '',
			title: '',
			RegistrationsDisabled: '',
      gateway: '',
			categories: [
				{ name: 'All', value: 'ALL', color: '#1565C0' }
			]
		},
		accountTabs: 0,
		accountModal: false,
    settingsTabs: 0,
    settingsModal: true,
    username: '',
		admin: false,
		koins: '',
    email: '',
    emailVerified: true,
    clientVersion: '0.177-stable',
    latestClientVersion: '',
    developerMode: false,
    theme: '',
    executive: false,
    bot: false,
    experimentsStore: null,

		token: null,
		passkey: "register",
    id: '',
    sessToken: '',

    show404Page: false,
    showConnModal: false,

		ajaxErrors: [],
		ajaxErrorsModal: false,

    conversations: [],

    MinQueryLength: 2
	},
	getters: {
		categoriesWithoutAll(state) {
			let categories =
				state.meta.categories
					.filter(category => ['ALL', 'OTHER'].indexOf(category.value) === -1)

			categories.unshift({
				name: 'Select a category',
				disabled: true
			})

			return categories.filter(category => category.value !== 'ALL')
		},
		title(state) {
			if (state.meta.title.trim().length) {
				return state.meta.title + ' - ' + state.meta.name
			} else {
				return state.meta.name
			}
		},
		alphabetizedCategories(state) {
			return state.meta.categories.sort((a, b) => {
				if (a.name === 'All') return -1
				if (a.name === 'Other' || b.name === 'Other') return -1

				if (a.name < b.name) {
					return -1
				} else if (a.name > b.name) {
					return 1
				}

				return 0
			})
		}
	},
	actions: {
		setTitle({ getters, state }, value) {
			document.title = getters.title
			state.meta.title = value
		}
	},
	mutations: {
		setToken(state, token) {
			state.token = token
		},
    setSessionToken(state, sessToken) {
      state.sessToken = sessToken
    },
    setLatestVersion(state, latestClientVersion) {
      state.latestClientVersion = latestClientVersion
    },
    setAPIVersion(state, latestAPIVersion) {
      state.latestAPIVersion = latestAPIVersion
    },
    setBot(state, bot) {state.bot = bot
    },
		setPassKey(state, passkey) {
			state.passkey = passkey
		},
    setTheme(state, theme) {
      state.theme = theme
    },
		set404Page(state, value) {
			state.show404Page = value
		},
		setAccountTabs(state, index) {
			state.accountTabs = index;
		},
    setSettingsTabs(state, index) {
      state.settingsTabs = index;
    },
		setSelectOptions(state, payload) {
			state.selectOptions[payload.name] = payload.value;
		},
		setAccountModalState(state, value) {
			state.accountModal = value;
		},
    setSettingsModalState(state, value) {
      state.settingsModal = value;
    },
    setGateway(state, value) {
      state.gateway = value;
    },
    setDevMode(state, value) {
      state.developerMode = value;
    },
    showConnModal(state, value) {
      state.connModal = value;
    },
    setExperimentsStore(state, value) {
      state.experimentsStore = value;
    },
		setAjaxErrorsModalState(state, value) {
			state.ajaxErrorsModal = value;
		},
		setAjaxErrors(state, value) {
			state.ajaxErrors = value
		},
		setUsername(state, value) {
			state.username = value
		},
		setKoins(state, value) {
			state.koins = value
		},
		setAdmin(state, value) {
			state.admin = value
		},
    setExecutive(state, value) {
      state.executive = value
    },
    setEmail(state, value) {
      state.email = value
    },
    setEmailVerified(state, value) {
      state.emailVerified = value
    },
    setUserId(state, value) {
      state.UserId = value
    },
		setSettings(state, value) {
			state.meta.name = value.siteName
			state.meta.description = value.siteDesc
			state.meta.showDescription = value.showDescription
			state.meta.bannerEnabled = value.bannerEnabled
			state.meta.bannerText = value.bannerText
			state.meta.RegistrationsDisabled = value.RegistrationsDisabled
      state.meta.logo = value.logo
      state.meta.icon = value.icon
		},
		addCategories(state, value) {
			if (Array.isArray(value)) {
				state.meta.categories.push(...value)
			} else {
				state.meta.categories.push(value)
			}
		},
		removeCategory(state, id) {
			let category = state.meta.categories.filter(c => c.id === id)
			let index = state.meta.categories.indexOf(category)

			state.meta.categories.splice(index, 1)
		},
		updateCategory(state, updated) {
			let category = state.meta.categories.filter(c => c.id === updated.id)
			let index = state.meta.categories.indexOf(category)

			state.meta.categories.splice(index, 1, updated)
		},
    // eslint-disable-next-line no-unused-vars
    clearConversations (state, conversations) {
      state.conversations = [];
    },
    addConversations (state, conversations) {
      state.conversations.push(...conversations);
    },
    updateConversationLastRead (state, id) {
      let index = state.conversations.findIndex(conversation => {
        return conversation.id === id;
      });

      let conversation = state.conversations[index];
      conversation.lastRead = new Date() + '';

      state.conversations.splice(index, 1, conversation);
    },
    updateUnshiftConversation (state, updatedConversation) {
      let index = state.conversations.findIndex(conversation => {
        return conversation.id === updatedConversation.id;
      });

      if(index > -1) {
        state.conversations.splice(index, 1);
      }

      state.conversations.unshift(updatedConversation);
    },
    updateConversationName (state, { id, name }) {
      let index = state.conversations.findIndex(conversation => {
        return conversation.id === id;
      });

      let conversation = state.conversations[index];
      conversation.name = name;

      state.conversations.splice(index, 1, conversation);
    }
	},
	modules: { thread, category, moderation }
})
