package org.jerrymouse.cc.site.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.jerrymouse.cc.site.bean.Resource;
import org.jerrymouse.cc.site.bean.Storage;
import org.jerrymouse.cc.site.bean.User;
import org.jerrymouse.cc.site.bean.UserConfig;
import org.jerrymouse.cc.site.store.StoreConfig;
import org.jerrymouse.cc.site.store.ftp.FtpStoreConfig;
import org.jerrymouse.cc.utils.ResourceBuilder;
import org.springframework.stereotype.Component;

@Component
public class UserDao extends BaseDao<User> {

	static {
		UserDao UserDao = new UserDao();
		UserDao.put(UserDao.createTestUser());
		UserDao.put(UserDao.createAnonymousUser());
	}

	public User createAnonymousUser() {
		User user = new User();
		user.setUsername("anonymous");
		user.setPassword("anonymous");
		UserConfig userConfig = new UserConfig();
		StoreConfig storeConfig = new StoreConfig();
		storeConfig.setType(StoreConfig.FILE);
		userConfig.setStoreConfig(storeConfig);
		user.setUserConfig(userConfig);
		return user;
	}

	public User createTestUser() {
		User user = new User();
		try {
			user.setUsername("yankai");
			user.setPassword("1248163264");
			Storage storage = new Storage();
			user.setStorage(storage);
			UserConfig userConfig = new UserConfig();
			StoreConfig storeConfig = new StoreConfig();
			storeConfig.setType(StoreConfig.FILE);
			// ftp
			FtpStoreConfig ftpStoreConfig = new FtpStoreConfig();
			ftpStoreConfig.setHost("localhost");
			ftpStoreConfig.setUsername("kaiyannju");
			ftpStoreConfig.setPassword("1248163264");
			ftpStoreConfig.setPath("/cc");
			storeConfig.setFtpStoreConfig(ftpStoreConfig);

			// cc
			userConfig.setStoreConfig(storeConfig);
			user.setUserConfig(userConfig);

			List<Resource> resources = new ArrayList<Resource>();
			Resource r1 = new Resource();
			r1.setOrgFullFilename("a.txt");
			r1.setUser(user);
			r1.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/a.txt")));
			Resource r2 = new Resource();
			r2.setOrgFullFilename("ººÎÄ.txt");
			r2.setUser(user);
			r2.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/ººÎÄ.txt")));
			Resource r3 = new Resource();
			r3.setOrgFullFilename("Cloud_computing.svg");
			r3.setUser(user);
			r3.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/Cloud_computing.svg")));
			Resource r4 = new Resource();
			r4.setOrgFullFilename("open_3d.mp4");
			r4.setUser(user);
			r4.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/open_3d.mp4")));
			Resource r5 = new Resource();
			r5.setOrgFullFilename("1.doc");
			r5.setUser(user);
			r5.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/1.doc")));
			Resource r6 = new Resource();
			r6.setOrgFullFilename("2.ppt");
			r6.setUser(user);
			r6.putBytes(IOUtils.toByteArray(ResourceBuilder
					.getResourcesBuilder().load("store/2.ppt")));
			resources.add(r1);
			resources.add(r2);
			resources.add(r3);
			resources.add(r4);
			resources.add(r5);
			resources.add(r6);
			storage.setResources(resources);
			user.setStorage(storage);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return user;
	}

	public User getUserByName(String username) {
		return get(username, User.class);
	}

}
