using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using Spaces;
using System.Collections.Generic;
using Spaces.Blog;

/// <summary>
/// BlogService 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.bfor.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class BlogService : System.Web.Services.WebService
{

    public BlogService()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string AddBlogCategory(string name)
    { 
 string account=SpacesContext.Current.Context.User.Identity.Name;
        if (account == null || account ==string.Empty)
        {
        
            return "[ {  id:-1,  name:'err'}]";
        }
        BlogCategory bc = new BlogCategory();
        UserConfig uc = Cacher.GetUserConfig(account);

        bc.BlogID = uc.BlogID;
        bc.ParentID = 0;
        bc.Title =HttpUtility.UrlDecode( name);
        bc.IsActive = true;
        bc.Description = string.Empty;
        bc.CategoryType = BlogCategoryType.PostCollection;
        BlogCategories.CreateBlogCategory(bc);
        List<BlogCategory> list = BlogCategories.GetBlogCategoryByAccount(account);
        StringBuilder sb = new StringBuilder();
        sb.Append("[");
        foreach (BlogCategory c in list)
        {
            sb.Append("{");
            sb.AppendFormat("id:{0},name:'{1}'",c.CategoryID,c.Title);
            sb.Append("},");
        
        }
        sb.Remove(sb.Length-1,1);
        sb.Append("]");
        return sb.ToString();
    /*[ {  id:1,  name:'someone1'},
 {  id:2,  name:'someone2'},
  {  id:3,  name:'someosf'},
   {  id:4,  name:'xxxx'}
 ]*/
    }
    [WebMethod]
    public string[] AddBlogClass(string name)
    {  
        string[] s;
        string account=SpacesContext.Current.Context.User.Identity.Name;
        if (account == null || account ==string.Empty)
        {
            s = new string[2];
            s[0] = "err";
            s[1] ="请登陆后在添加分类！";
            return s;
        }
        BlogCategory bc = new BlogCategory();
        UserConfig uc = Cacher.GetUserConfig(account);
   
        bc.BlogID = uc.BlogID;
        bc.ParentID = 0;
        bc.Title = name;
        bc.IsActive = true;
        bc.Description = string.Empty;
        bc.CategoryType = BlogCategoryType.PostCollection;
        BlogCategories.CreateBlogCategory(bc);
     List<BlogCategory> list=  BlogCategories.GetBlogCategoryByAccount(account);
     int i = list.Count * 2;
     int k = 0;
     if (i > 0)
     {
         s = new string[i];
         for (int j = 0; j < list.Count; j++)
         {
             s[k] = list[j].CategoryID.ToString();
             s[k + 1] = list[j].Title.ToString();
             k = k + 2;
         }
         return s;
     }
     return null;
    }
    [WebMethod]
    public string Test()
    {
        return "哈哈";
    }
    [WebMethod]
    public string Test1(string Test)
    {
        return "哈哈";
    }
    [WebMethod]
    public string CheckUsername(string username)
    {
        if (Users.UserExists(username))
            return "该用户已存在！";
        else
            return "您可以注册该用户名！";
    }
    [WebMethod]
    public string CheckAccount(string Account)
    {
        if (Account.Length < 3)
        {
            return Globals.Text["Service_CheckAccountLength"];
        }
        if (!Utils.CheckAccount(Account))
        {
            return Globals.Text["Service_CheckAccount"];
        }
       // DiskData data = new DiskData();
        if (SiteTools.IsRegisterDisallowed(Account))
        {
            return Globals.Messages[(int)MessageType.RegisterBanned].Body;
        }
        if (Spaces.SpacesDataProvider.Instance() .AccountExists(Account))
        {
            return Globals.Text["Service_AccountExists"];
        }
        else
        {
            return Globals.Text["Service_AccountOK"];
        }
    }

}

