using System;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using Model.Ad;
using System.Web;
namespace BLL.Ad
{
    /// <summary>
    /// CreatJs 的摘要说明
    /// </summary>
    public class CreatJs
    {
        public CreatJs() { }
        public  void CreatJsFile(AdItiem adi,AdMemberInfo adm) 
        {
            string path = HttpContext.Current.Server.MapPath("~/js/Advertisement/" + adi.Id.ToString() + ".js");
            Encoding codi = Encoding.GetEncoding("gb2312");
            StringBuilder strb = new StringBuilder();
            StreamReader read=null;
            string st = null;
            if (adi.Type == "全屏浮动显示") 
            {
                try
                {
                    read = new StreamReader(HttpContext.Current.Server.MapPath("~/js/001.js"), codi);
                    st = read.ReadToEnd();
                }
                catch (Exception e)
                {
                    HttpContext.Current.Response.Write(e.Message);
                    HttpContext.Current.Response.End();
                }
                qPfd(adi,adm,strb);
                strb.Append(st);
            }
            else if (adi.Type == "上下浮动显示-右") 
            {
                try
                {
                    read = new StreamReader(HttpContext.Current.Server.MapPath("~/js/004.js"), codi);
                    st = read.ReadToEnd();
                }
                catch(Exception e) 
                {
                    HttpContext.Current.Response.Write(e.Message);
                    HttpContext.Current.Response.End();
                }
                rightP(adi,adm,strb);
                strb.Append(st);
            }
            else if (adi.Type == "上下浮动显示-左")
            {
                try
                {
                    read = new StreamReader(HttpContext.Current.Server.MapPath("~/js/003.js"), codi);
                    st = read.ReadToEnd();
                }
                catch(Exception e) 
                {
                    HttpContext.Current.Response.Write(e.Message);
                    HttpContext.Current.Response.End();
                }
                left(adi,adm,strb);
                strb.Append(st);
            }
            else if (adi.Type == "对联式广告")
            {
                try
                {
                    read = new StreamReader(HttpContext.Current.Server.MapPath("~/js/002.js"), codi);
                    st = read.ReadToEnd();
                }
                catch (Exception e)
                {
                    HttpContext.Current.Response.Write(e.Message);
                    HttpContext.Current.Response.End();
                }
                couplet(adi,adm,strb);
                strb.Append(st);
            }
            else if (adi.Type == "全屏渐影消失")
            {
                try
                {
                    read = new StreamReader(HttpContext.Current.Server.MapPath("~/js/jyxs.js"), codi);
                    st = read.ReadToEnd();
                }
                catch (Exception e)
                {
                    HttpContext.Current.Response.Write(e.Message);
                    HttpContext.Current.Response.End();
                }
                jyxs(adi,adm,strb);
                strb.Append(st);
            }
            else if (adi.Type == "普通显示") 
            {
                common(adi,adm,strb);
            }
            
            StreamWriter wri = null;
            try
            {
                
                Stream output = new FileStream(path, FileMode.Create);
                wri = new StreamWriter(output);
                wri.Write(strb);
                wri.Flush();
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write(e.Message);
                HttpContext.Current.Response.End();
               
            }
            finally
            {
                wri.Close();
                if (adi.Type != "普通显示")
                {
                    read.Close();
                }
            }
            
           
        }
        /// <summary>
        /// 普通显示
        /// </summary>
        /// <returns></returns>
        private StringBuilder common(AdItiem adi,AdMemberInfo adm,StringBuilder strb) 
        {
            strb.Append("document.write(\"");
            strb.Append("<a href=\'" + adm.M_Adrees.ToString() + "\'>");
            strb.Append("<img src=\'" + adm.MImageAdrees.ToString() + "\' border=\'0\'></a>\");");
            return strb;
        }

        /// <summary>
        /// 全屏浮动式显示
        /// </summary>
        private StringBuilder qPfd(AdItiem adi,AdMemberInfo adm,StringBuilder strb)
        {
            strb.Append("document.write(\"<div id=\'img\' style=\'position:absolute; width:" + adi.AdWidth.ToString() + "; height:" + adi.AdHeight.ToString() + "\'>");
            strb.Append("<a href=\'" + adm.M_Adrees.ToString()+ "\'>");
            strb.Append("<img src=\'" +adm.MImageAdrees.ToString()+ "\' border=\'0\'></a></div>\");");
            return strb;
        }
        /// <summary>
        /// 右窗体边显示
        /// </summary>
        private  StringBuilder rightP(AdItiem adi,AdMemberInfo adm,StringBuilder strb) 
        {
            strb.Append("document.write('");
            strb.Append("<div id=\"img\" style=\"position:absolute; width:" + adi.AdWidth.ToString() + "; height:" + adi.AdHeight.ToString() + "\">");
            strb.Append("<a href=\"" + adm.M_Adrees.ToString() + "\">");
            strb.Append("<img src=\"" + adm.MImageAdrees.ToString() + "\" border=\"0\"></a></div>');");
            return strb;
        }
        /// <summary>
        /// 左窗体边显示
        /// </summary>
        private StringBuilder left(AdItiem adi,AdMemberInfo adm,StringBuilder strb) 
        {
            strb.Append("document.write('");
            strb.Append("<div id=\"img\" style=\"position:absolute; width:" + adi.AdWidth.ToString() + "; height:" + adi.AdHeight.ToString() + "\">");
            strb.Append("<a href=\"" + adm.M_Adrees.ToString() + "\">");
            strb.Append("<img src=\"" + adm.MImageAdrees.ToString() + "\" border=\"0\"></a></div>');");
            return strb;
        }
        /// <summary>
        /// 渐影消失
        /// </summary>
        /// <returns></returns>
        private StringBuilder jyxs(AdItiem adi, AdMemberInfo adm, StringBuilder strb)
        {
            strb.AppendLine("document.writeln('<div align=\"center\" id=\"miman\">');");
            strb.AppendLine("document.writeln('<table width=\"760\" border=\"0\" cellspacing=\"0\"  cellpadding=\"0\">');");
            strb.AppendLine("document.writeln('<tr>');");
            strb.AppendLine("document.writeln('<td><a href=\""+adm.M_Adrees.ToString()+"\"  target=\"_blank\"><img onload=\"javascript:log_start()\"  src=\""+adm.MImageAdrees.ToString()+"\"  width=\""+adi.AdWidth.ToString()+"\"  height=\""+adi.AdHeight.ToString()+"\"  border=\"0\"></a></td>');");
            strb.AppendLine("document.writeln('</tr>');");
            strb.AppendLine("document.writeln('</table>');");
            strb.AppendLine("document.writeln('</div>');");
            
            return strb;
        }
        /// <summary>
        /// 对联式广告
        /// </summary>
        /// <returns></returns>
        private StringBuilder couplet(AdItiem adi,AdMemberInfo adm,StringBuilder strb) 
        {
            strb.Append("document.write(\"<div id=AdLayer1 style='position: absolute;visibility:hidden;z-index:1'><a href=\'"+adm.M_Adrees.ToString());
            strb.Append("\'><img src="+adm.MImageAdrees.ToString()+"  ");
            strb.Append(" width=\'"+adi.AdWidth.ToString()+"\'  ");
            strb.Append("height=\'"+adi.AdHeight.ToString()+"\' ");
            strb.Append(" border=\'0\'></a></div>\"+");
            strb.Append("\"<div id=AdLayer2 style=\'position: absolute;visibility:hidden;z-index:1\'><a href=\'"+adm.M_Adrees.ToString()+"\'");
            strb.Append("><img src="+adm.MImageAdrees.ToString()+" ");
            strb.Append(" width=\'" + adi.AdWidth.ToString() + "\'  ");
            strb.Append("height=\'" + adi.AdHeight.ToString() + "\' ");
            strb.Append("  border=\'0\'></a></div>\");");
            return strb;
        }
    }
}
