using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Spaces;
using System.IO;
using Xiaoxiao.Upload;
using System.Drawing;
using System.Drawing.Imaging;
using LanguagePack;

public partial class Admin_PageSet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindConfig();
    }
    void BindConfig()
    {
     
        UserConfig uc = Cacher.GetUserConfig(Account);
        this.txtTitle.Text = uc.Title;
       // lblAccount.Text = uc.Account;

        chkUpdatePicture.Checked = false;

        if (uc.AvatarImage == null || uc.AvatarImage == string.Empty)
        {
            Picture.ImageUrl = string.Format("{0}/images/AvatarImage.gif",Globals.ApplicationPath);
        }
        else
        {
            Picture.ImageUrl = string.Format("{0}/{1}/Avatar/{2}",Config.GetConfig().BlogAlbumUrl,uc.Account,uc.AvatarImage);
        }
        Description.Text = uc.Description;
       
        chkUpdatePicture.Text = Globals.Text["PageSet_ModifyPicture"];
        buttonUpdate.Text = Globals.Text["PageSet_ModifySetting"];
        dropLanguages.Items.Clear();
        foreach (string s in Resources.SupportLanguages.Keys)
        {
            ListItem li = new ListItem(Resources.SupportLanguages[s],s );
            if (li.Value == uc.Language)
            {
                li.Selected = true;
            }
            dropLanguages.Items.Add(li);
        }

    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
       UserConfig uc = Cacher.GetUserConfig(Account);
        uc.Title = this.txtTitle.Text;
        uc.Description = Description.Text;
        UploadFile uploadFile = UploadHelper.GetUploadFile("file1");
        if (chkUpdatePicture.Checked)
        {
            if (uploadFile == null)
            {
               Message.Text= Msg.GetMsg("你选择了上传|更改 图片，但没有选择图片文件!");
                return;
            }
            if (!checkFile(uploadFile.ContentType))
            {
                Message.Text = Msg.GetMsg("只可以上传图片文件!");
                return;
            }
          
            string path = Config.GetConfig().BlogAlbumPath;
            path = Path.Combine(path, uc.Account);
            path = Path.Combine(path,  "Avatar");
            if (!path.EndsWith("\\"))
            {
                path = string.Format("{0}\\",path);
            }
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            string fileName = Path.GetFileName(uploadFile.FileName);
           
            uploadFile.SaveAs(Path.Combine(path, fileName));
            uc.AvatarImage = Utils.GetThumbnailImage(Path.Combine(path, fileName),150,150); //SaveImg(Path.Combine(path, fileName));
        }

        if (uc.Layout == null)
        {

            Theme the = Globals.GetThemes().FindTheme(uc.Theme);
            UserLayout ul = new UserLayout();
            ul.Theme = the.Name;
            ul.Style = the.Style;
       
            foreach (ThemeRow tr in the.Rows)
            {
                ul.Rows.Add(tr);
            }

            uc.Layout = ul;
        }
        uc.Language = dropLanguages.SelectedValue;
        Users.UpdateUserConfig(uc);
        BindConfig();
        Message.Text = Msg.GetMsg("您已成功更改页面设置!");
       
        
    }
  

    bool checkFile(string type)
    {
        bool chk = false;
        switch (type)
        {
            case "image/gif":
                chk=true;
                break;
            case "image/jpeg":
            case "image/pjpeg":
                chk = true;
                break;
            case "image/bmp":
                chk = true;
                break;
            case "image/x-png":
                chk = true;
                break;
            default:
                chk = false;
                break;


        }
        return chk;
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
   
}
