using System;
using System.Collections.Generic;
using System.Text;

namespace ChuPiao.Common.Utils
{
    public class MachineReportTimeList
    {
        /// <summary>
        /// 获取报表的时段列表，核心是自然周的计算
        /// </summary>
        /// <param name="timetype">1-天，2-周，3-月</param>
        /// <returns></returns>
        public static List<MachineReportTime> GetTimeList(int timetype)
        {
            DateTime now = DateTime.Now.AddHours(-7);   //当日7点到次日7点算当天，即7点前算前一天

            List<MachineReportTime> timelist = new List<MachineReportTime>();
            DateTime from;
            DateTime to;
            if (timetype == 1)
            {
                //当天前一月的日期
                from = now;
                to = DateTime.Parse(now.AddMonths(-1).ToString("yyyy-MM-01"));
                for (DateTime date = from; date > to; date = date.AddDays(-1))
                {
                    MachineReportTime entity = new MachineReportTime();
                    entity.DisplayMember = date.ToString("yyyy年MM月dd日");
                    entity.ValueMember = date.ToString("yyyyMMdd");
                    entity.From = date;
                    entity.To = date;

                    timelist.Add(entity);
                }
            }
            else if (timetype == 2)
            {
                //当周前三月的周
                to = DateTime.Parse(now.ToString("yyyy-MM-dd"));
                from = to.AddMonths(-2);
                for (DateTime date = from; date <= to; date = date.AddMonths(1))
                {
                    DateTime monthfrom = DateTime.Parse(date.ToString("yyyy-MM-01"));
                    DateTime monthto = monthfrom.AddMonths(1).AddDays(-1);
                    DateTime weekfrom = monthfrom;
                    DateTime weekto;
                    for (int i = 1; ; i++)
                    {
                        int dayofweek = (int)(weekfrom.DayOfWeek) == 0 ? 7 : (int)(weekfrom.DayOfWeek);
                        weekto = weekfrom.AddDays(7 - dayofweek);

                        //算出的自然周的结尾超过自然月的结尾的以自然月的结尾作为自然周的结尾
                        if (weekto > monthto)
                        {
                            weekto = monthto;
                            //if (to < monthto)
                            //{
                            //    weekto = to;
                            //}

                            MachineReportTime entity1 = new MachineReportTime();
                            entity1.DisplayMember = string.Concat(date.ToString("yyyy年MM月第"), i, "周");
                            entity1.ValueMember = string.Concat(date.ToString("yyyyMM-"), i);
                            entity1.From = weekfrom;
                            entity1.To = weekto;
                            timelist.Add(entity1);

                            break;
                        }

                        ////如果算出的自然周的结尾超过了当天，那这周不算
                        //if (weekto > to)
                        //{
                        //    weekto = to;

                        //    MachineReportTime entity2 = new MachineReportTime();
                        //    entity2.DisplayMember = string.Concat(date.ToString("yyyy年MM月第 "), i, " 周");
                        //    entity2.ValueMember = string.Concat(date.ToString("yyyyMM-"), i);
                        //    entity2.From = weekfrom;
                        //    entity2.To = weekto;
                        //    timelist.Add(entity2);

                        //    break;
                        //}

                        MachineReportTime entity = new MachineReportTime();
                        entity.DisplayMember = string.Concat(date.ToString("yyyy年MM月第"), i, "周");
                        entity.ValueMember = string.Concat(date.ToString("yyyyMM-"), i);
                        entity.From = weekfrom;
                        entity.To = weekto;
                        timelist.Add(entity);

                        weekfrom = weekto.AddDays(1);
                        //2014.8月
                        if (weekfrom > monthto)
                        {
                            break;
                        }

                        if (weekfrom > to)
                        {
                            break;
                        }
                    }
                }
                timelist.Reverse();
            }
            else if (timetype == 3)
            {
                //当月的第一天
                from = DateTime.Parse(now.ToString("yyyy-MM-01"));
                to = from.AddMonths(-3);
                for (DateTime date = from; date >= to; date = date.AddMonths(-1))
                {
                    MachineReportTime entity = new MachineReportTime();
                    entity.DisplayMember = date.ToString("yyyy年MM月");
                    entity.ValueMember = date.ToString("yyyyMM");
                    entity.From = date;
                    entity.To = date.AddMonths(1).AddDays(-1);

                    timelist.Add(entity);
                }
            }
            return timelist;
        }

        /// <summary>
        /// 获取上个时间段的名称
        /// </summary>
        /// <param name="timetype">1-天，2-周，3-月</param>
        /// <returns></returns>
        public static MachineReportTime GetCurrentTime(int timetype)
        {
            return GetTimeList(timetype)[0];
        }
    }

    public class MachineReportTime
    {
        private string displaymember;
        public string DisplayMember
        {
            get { return displaymember; }
            set { displaymember = value; }
        }
        private string valuemember;
        public string ValueMember
        {
            get { return valuemember; }
            set { valuemember = value; }
        }
        private DateTime from;
        public DateTime From
        {
            get { return from; }
            set { from = value; }
        }
        private DateTime to;
        public DateTime To
        {
            get { return to; }
            set { to = value; }
        }
    }
}
