package com.uas.account.dao;

import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.uas.account.entity.App;
import com.uas.account.entity.App.Scope;
import com.uas.account.viewmodel.AppUseView;
import com.uas.account.viewmodel.AppView;

@Repository
public class AppDaoImpl extends BaseDao implements AppDao {

	@Override
	public App save(App app) {
		int count = count("select count(1) from ac$apps where uid_=?", app.getUid());
		if (count == 0) {
			jdbcTemplate.update(
					"insert into ac$apps(uid_,publicKey,description,backSpaceUrl,backUserUrl,backUserPwdUrl,loginUrl,logoutUrl,encryFormat,copyApps,userControl,usCount,scope_,defaultUse,dialectEnable,homePage,personalEnable,backChangeAdminUrl) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					app.getUid(), app.getPublicKey(), app.getDescription(), app.getBackSpaceUrl(), app.getBackUserUrl(),
					app.getBackUserPwdUrl(), app.getLoginUrl(), app.getLogoutUrl(), app.getEncryFormat(), app.getCopyApps(),
					app.getUserControl(), app.getUsCount(), app.getScope(), app.getDefaultUse(), app.getDialectEnable(),
					app.getHomePage(), app.getPersonalEnable(), app.getBackChangeAdminUrl());
		} else {
			jdbcTemplate.update(
					"update ac$apps set description=?,publicKey=?,backSpaceUrl=?,backUserUrl=?,backUserPwdUrl=?,loginUrl=?,logoutUrl=?,encryFormat=?,copyApps=?,userControl=?,usCount=?,scope_=?,defaultUse=?,dialectEnable=?,homePage=?,personalEnable=?,backChangeAdminUrl=? where uid_=?",
					app.getDescription(), app.getPublicKey(), app.getBackSpaceUrl(), app.getBackUserUrl(),
					app.getBackUserPwdUrl(), app.getLoginUrl(), app.getLogoutUrl(), app.getEncryFormat(), app.getCopyApps(),
					app.getUserControl(), app.getUsCount(), app.getScope(), app.getDefaultUse(), app.getDialectEnable(),
					app.getHomePage(), app.getPersonalEnable(), app.getBackChangeAdminUrl(), app.getUid());
		}
		return app;
	}

	@Override
	public App findOne(String appId) {
		try {
			return jdbcTemplate.queryForObject(
					"select uid_ as \"UID\",publicKey,description,backSpaceUrl,backUserUrl,backUserPwdUrl,loginUrl,logoutUrl,encryFormat,copyApps,userControl,usCount,scope_ as \"SCOPE\",defaultUse,dialectEnable,homePage,personalEnable,backChangeAdminUrl,backpartnerurl from ac$apps where uid_=?",
					new BeanPropertyRowMapper<App>(App.class), appId);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<String> findAllIDs() {
		try {
			return jdbcTemplate.queryForList("select distinct uid_ from ac$apps order by uid_", String.class);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<String> findAllIDsBySpace(long spaceId) {
		try {
			return jdbcTemplate.queryForList("select distinct appId from ac$userspaces where id=? order by appId",
					String.class, spaceId);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<String> findAllIDsByUser(long userId) {
		try {
			return jdbcTemplate.queryForList("select distinct appId from ac$users where id=? order by appId",
					String.class, userId);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<App> findAll() {
		try {
			return jdbcTemplate.query(
					"select uid_ as \"UID\",publicKey,description,backSpaceUrl,backUserUrl,backUserPwdUrl,encryFormat,copyApps,userControl,usCount,scope_ as \"SCOPE\",defaultUse,dialectEnable,homePage,personalEnable,backChangeAdminUrl,loginUrl,logoutUrl from ac$apps order by uid_",
					new BeanPropertyRowMapper<App>(App.class));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<AppView> findAllView() {
		try {
			return jdbcTemplate.query("select uid_ as \"UID\",description,usCount from ac$apps order by uid_",
					new BeanPropertyRowMapper<AppView>(AppView.class));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public void delete(String appId) {
		jdbcTemplate.update("delete from ac$apps where uid_=?", appId);
	}

	@Override
	public List<AppUseView> findAllPublicUseView() {
		try {
			return jdbcTemplate.query(
					"select uid_ as \"UID\",description,defaultUse from ac$apps where (scope_=? or scope_=?) and userControl is null and backSpaceUrl is not null order by usCount,uid_",
					new BeanPropertyRowMapper<AppUseView>(AppUseView.class), Scope.PUBLIC.name(),
					Scope.PROTECTED.name());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

}
