using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LYFZ.MandaoSmsInterface
{
    public partial class frmAccountInformation : LYFZ.ComponentLibrary.BaseContentsFormMain
    { //  000/lyfz/9340.8260/0.0460/70//0.2000/0.3000/0.3000/winic
        //000/用户名/余额/单价/短信字符数/签名/语音短信单价/传真短信单价/彩信单价/所属经营商
        public frmAccountInformation()
        {
            InitializeComponent();
            this.Shown += frmAccountInformation_Shown;
            this.chkOpen.Click += chkOpen_Click;
        }

        void chkOpen_Click(object sender, EventArgs e)
        {
            if (this.rdobtnSingleShot.Checked)
            {
                this.CurrentSMSAccountInformation.IsSingleOpen = this.chkOpen.Checked;
            }
            else
            {
                this.CurrentSMSAccountInformation.IsBulkOpen = this.chkOpen.Checked;
            }
        }
     /*   string _Account = "";
        /// <summary>
        /// 帐号
        /// </summary>
        public string Account
        {
            get { return _Account; }
            set { _Account = value; }
        }
        string _Password = "";
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }*/

        /// <summary>
        /// 帐号
        /// </summary>
        string Account = "";

        /// <summary>
        /// 密码
        /// </summary>
        string Password = "";

        /// <summary>
        /// 旧的帐号信息
        /// </summary>
        string oldAccountInfo = "";

        public frmAccountInformation(LYFZ.Model.Model_SMSAccountInformation sMSAccountInformation)
            :this()
        {
            this.currentSMSAccountInformation = sMSAccountInformation;
            this.skinTextBoxAccount.Text = sMSAccountInformation.SingleUid;
            this.skinTextBoxPassword.Text =sMSAccountInformation.SinglePwd;
            this.skinTextBox1.Text = sMSAccountInformation.SingleRechargeRemindbalance.ToString();
            this.skinTextBox2.Text = sMSAccountInformation.SingleRechargeRemindPhone;
            this.nudCount.Value = sMSAccountInformation.SwitchingCondition;
            this.chkOpen.Checked = sMSAccountInformation.IsSingleOpen;
            this.Account = this.skinTextBoxAccount.Text;
            this.Password = this.skinTextBoxPassword.Text;
            this.oldAccountInfo = sMSAccountInformation.SingleUid
                + sMSAccountInformation.SinglePwd 
                + sMSAccountInformation.SwitchingCondition 
                + sMSAccountInformation.SingleServiceNumber
                + sMSAccountInformation.SingleRechargeRemindbalance.ToString()
                + sMSAccountInformation.SingleRechargeRemindPhone
                + sMSAccountInformation.BulkUid
                + sMSAccountInformation.BulkPwd
                + sMSAccountInformation.BulkServiceNumber
                + sMSAccountInformation.BulkRechargeRemindbalance.ToString()
                + this.currentSMSAccountInformation.IsSingleOpen.ToString()
                + this.currentSMSAccountInformation.IsBulkOpen.ToString()
                + sMSAccountInformation.BulkRechargeRemindPhone;
        }
        /// <summary>
        /// 是否有更新帐号
        /// </summary>
        public bool GetIsUpdateAccount()
        {
            if (this.oldAccountInfo != (this.currentSMSAccountInformation.SingleUid 
                + this.currentSMSAccountInformation.SinglePwd 
                + this.currentSMSAccountInformation.SwitchingCondition 
                + this.currentSMSAccountInformation.SingleServiceNumber
                + this.currentSMSAccountInformation.SingleRechargeRemindbalance.ToString()
                + this.currentSMSAccountInformation.SingleRechargeRemindPhone
                + this.currentSMSAccountInformation.BulkUid
                + this.currentSMSAccountInformation.BulkPwd
                + this.currentSMSAccountInformation.BulkServiceNumber
                + this.currentSMSAccountInformation.BulkRechargeRemindbalance.ToString()
                + this.currentSMSAccountInformation.IsSingleOpen.ToString()
                + this.currentSMSAccountInformation.IsBulkOpen.ToString()
                + this.currentSMSAccountInformation.BulkRechargeRemindPhone))
            {
                return true;
            }
            else {
                return false;
            }
        }

        LYFZ.Model.Model_SMSAccountInformation currentSMSAccountInformation = new Model.Model_SMSAccountInformation();
        /// <summary>
        /// 短信接口帐号信息
        /// </summary>
        public LYFZ.Model.Model_SMSAccountInformation CurrentSMSAccountInformation
        {
            get { return currentSMSAccountInformation; }
            set { currentSMSAccountInformation = value; }
        }

        //创建一个委托,是为访问控件服务的。
        public delegate void UpdateControls();
        void frmAccountInformation_Shown(object sender, EventArgs e)
        {
            LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker)
            {
                try
                {
                    SmsUserInfo userinfo = new SmsUserInfo(HTTPSmsInterface.GetBalances(this.Account, this.Password), this.Account);

                    this.Invoke(new UpdateControls(delegate()
                    {
                        lab查询结果.Text = "查询" + userinfo.ReturnStatus;
                        lb用户名.Text = this.Account;
                        lb余额.Text = userinfo.Balance;
                        // lb单价.Text = userinfo.Price.ToString();
                          lb短信字符数.Text = userinfo.SMSCharacterCount.ToString();
                        lb短信签名.Text = userinfo.Signatures;
                        
                        // lb语音单价.Text = userinfo.VoiceMessagingPrice.ToString();
                        //  lb彩信单价.Text = userinfo.MMSPrice.ToString();
                        // lb传真单价.Text = userinfo.FaxMessagingPrice.ToString();
                    }));

                }
                catch (Exception ex)
                {
                    this.Invoke(new UpdateControls(delegate()
                    {
                        lab查询结果.Text = "帐号余额查询失败";
                    }));


                    MessageBoxCustom.Show("帐号余额查询失败," + ex.Message, backgroundWorker: backgroundWorker);
                }
            });

        }


        private void skinButtomQuery_Click(object sender, EventArgs e)
        {
            this.Account = this.skinTextBoxAccount.Text;
            this.Password = this.skinTextBoxPassword.Text;
            this.CurrentSMSAccountInformation.SwitchingCondition = Convert.ToInt32(this.nudCount.Value);
            if (this.rdobtnSingleShot.Checked)
            {
                this.CurrentSMSAccountInformation.SingleUid = this.skinTextBoxAccount.Text.Trim();
                this.CurrentSMSAccountInformation.SinglePwd = this.skinTextBoxPassword.Text.Trim();
                //this.CurrentSMSAccountInformation.SingleRechargeRemindbalance = Convert.ToInt32(this.skinTextBox1.Text.Trim());
                //this.CurrentSMSAccountInformation.SingleRechargeRemindPhone = this.skinTextBox2.Text.Trim();
            }
            else {
                this.CurrentSMSAccountInformation.BulkUid = this.skinTextBoxAccount.Text.Trim();
                this.CurrentSMSAccountInformation.BulkPwd = this.skinTextBoxPassword.Text.Trim();
                //this.CurrentSMSAccountInformation.BulkRechargeRemindbalance = Convert.ToInt32(this.skinTextBox1.Text.Trim());
                //this.CurrentSMSAccountInformation.BulkRechargeRemindPhone = this.skinTextBox2.Text.Trim();
            }
            frmAccountInformation_Shown(null, null);
        }
        private void skinButtomSendSms_Click(object sender, EventArgs e)
        {

            this.Account = this.skinTextBoxAccount.Text.Trim();
            this.Password = this.skinTextBoxPassword.Text.Trim();
            string RecipientPhone = this.skinTextBoxPhoneNumber.Text.Trim();
            string SMSContent = this.skinTextBoxSmsInfo.Text.Trim();

            string ErrorPhone = LYFZ.WinAPI.CustomPublicMethod.ValidationPhones(RecipientPhone,ref RecipientPhone);
            if (ErrorPhone.Length > 0)
            {
                MessageBoxCustom.Show("请求发送手机号格式不正确,以下为错误手机号:\r\n" + ErrorPhone);
                return;
            }

            string[] phones = RecipientPhone.Split(',');
            if (phones.Length > 100)
            {
                MessageBoxCustom.Show("请求发送手机号过多,一次只能同时发送给100个手机号,当前请求手机号个数:" + phones.Length);
                return;
            }
            if (SMSContent.Length > 300)
            {
                MessageBoxCustom.Show("请求发送短信长度为:" + SMSContent.Length + "个字符,测试发送短信内容超长,不能发送。");
                return;
            }
            if (SMSContent.Length <= 0)
            {
                MessageBoxCustom.Show("短信内容不能为空。");
                return;
            }
            LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker)
            {
                try
                {
                    HTTPSmsSendReturnInfo sendInfo = HTTPSmsInterface.SendSmsMessages(this.Account, this.Password, RecipientPhone, SMSContent);//new HTTPSmsSendReturnInfo("000/Send:1/Consumption:.046/Tmoney:7982.446/sid:1029101707233851");

                    try
                    {
                        if (sendInfo.ReturnStatus.Contains("成功"))
                        {
                            LYFZ.DAL.tb_SMSSendRecords smsDal = new DAL.tb_SMSSendRecords();
                            LYFZ.Model.tb_SMSSendRecords smsModel = new Model.tb_SMSSendRecords();
                            smsModel.SMSAccount = this.Account;
                            smsModel.CustomerID = smsDal.GetSendRecordID(this.Account);
                            smsModel.RecipientPhone = RecipientPhone;
                            smsModel.SMSContent = SMSContent;
                            smsModel.Status = SendSmsResults.SendStatus.发送成功.ToString();
                            smsModel.SMSCustomerDateTime = DateTime.Now;
                            smsModel.RecipientDateTime = DateTime.Now;
                            smsModel.SendDateTime = DateTime.Now;
                            smsModel.AgentChargeback = sendInfo.SendCount;
                            smsModel.OperatorChargeback = sendInfo.SendCount;
                            smsModel.AgentPrice = sendInfo.Price;
                            smsModel.OperatorPrice = smsModel.AgentPrice;
                            smsModel.SendLogs = sendInfo.SourceString;
                            smsModel.SendFailureNumber = 0;
                            smsModel.PartialFailurePhone = "";
                            smsModel.WaitingToSend = "";
                            smsModel.SMSInterface = "北京漫道";
                            smsModel.CustomerQueries = "1";
                            string md5Key = LYFZ.WinAPI.SDKSecurity.MD5Encrypt(smsModel.RecipientPhone + smsModel.SMSContent + smsModel.SMSCustomerDateTime);
                            smsModel.MD5KEY = md5Key;
                            smsDal.Add(smsModel);
                        }
                    }
                    catch { sendInfo.ReturnStatus = "发送成功,但记录插入错误"; }


                    this.Invoke(new UpdateControls(delegate()
                    {
                        this.labelEx发送结果.Text = sendInfo.ReturnStatus + "\r\n" + sendInfo.SourceString;
                    }));

                }
                catch (Exception ex)
                {

                    MessageBoxCustom.Show("发送错误," + ex.Message, backgroundWorker: backgroundWorker);
                }
            });
        }
      /*  int rechargeRemindbalance = 1000;
        /// <summary>
        /// 充值提醒余额 (条)
        /// </summary>
        public int RechargeRemindbalance
        {
            get { return rechargeRemindbalance; }
            set { rechargeRemindbalance = value; }
        }
        string rechargeRemindPhone = "";
        /// <summary>
        /// 充值提醒短信接收手机号
        /// </summary>
        public string RechargeRemindPhone
        {
            get { return rechargeRemindPhone; }
            set { rechargeRemindPhone = value; }
        }*/
        private void skinButtom1_Click(object sender, EventArgs e)
        {
            
            try
            {
                //this.CurrentSMSAccountInformation.SingleRechargeRemindbalance =Convert.ToInt32(this.skinTextBox1.Text.Trim());
               // this.CurrentSMSAccountInformation.SingleRechargeRemindPhone = this.skinTextBox2.Text.Trim();
                this.CurrentSMSAccountInformation.SwitchingCondition = Convert.ToInt32(this.nudCount.Value);
                if (this.rdobtnSingleShot.Checked)
                {
                    this.CurrentSMSAccountInformation.SingleRechargeRemindbalance = Convert.ToInt32(this.skinTextBox1.Text.Trim());
                    this.CurrentSMSAccountInformation.SingleRechargeRemindPhone = this.skinTextBox2.Text.Trim();
                }
                else
                {
                    this.CurrentSMSAccountInformation.BulkRechargeRemindbalance = Convert.ToInt32(this.skinTextBox1.Text.Trim());
                    this.CurrentSMSAccountInformation.BulkRechargeRemindPhone = this.skinTextBox2.Text.Trim();
                }
                MessageBoxCustom.Show("保存成功");
            }
            catch {
                MessageBoxCustom.Show("输入信息不正确");
            }
        }
        /// <summary>
        /// 单发通道 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdobtnSingleShot_Click(object sender, EventArgs e)
        {
            this.skinTextBoxAccount.Text = this.CurrentSMSAccountInformation.SingleUid; 
            this.skinTextBoxPassword.Text = this.CurrentSMSAccountInformation.SinglePwd;
            this.skinTextBox1.Text = this.CurrentSMSAccountInformation.SingleRechargeRemindbalance.ToString();
            this.skinTextBox2.Text = this.CurrentSMSAccountInformation.SingleRechargeRemindPhone;
            this.chkOpen.Checked = this.CurrentSMSAccountInformation.IsSingleOpen;
            skinButtomQuery_Click(sender, e);
        }
        /// <summary>
        /// 群发通单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdobtnBulk_Click(object sender, EventArgs e)
        {
            this.skinTextBoxAccount.Text = this.CurrentSMSAccountInformation.BulkUid;
            this.skinTextBoxPassword.Text = this.CurrentSMSAccountInformation.BulkPwd;
            this.skinTextBox1.Text = this.CurrentSMSAccountInformation.BulkRechargeRemindbalance.ToString();
            this.skinTextBox2.Text = this.CurrentSMSAccountInformation.BulkRechargeRemindPhone;
            this.chkOpen.Checked = this.CurrentSMSAccountInformation.IsBulkOpen;
            skinButtomQuery_Click(sender, e);
        }


    }
}