using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using DataDynamics.ActiveReports;
using DataDynamics.ActiveReports.Document;
using System.Data;
using System.Windows.Forms;
using System.IO;
using DataDynamics.ActiveReports.Design;
using System.Reflection;
namespace IControls_FireManager
{
    ///     
    /// 동적으로 데이타를 처리하는 레포트이다. 실제로 데이타베이스에서 가져오든, 혹은 사용자가 직접 생성한 데이타를 ArrayList 혹은 DataColection 으로 받고
    /// 그 데이타를 컬럼화 하여 직접 레포트를 구성하여 출력하도록 한다. 또한 데이타 추출을 위해 Reflection 을 적용하여 타입 및 클래스 멤버를 추출한다
    /// 
    
    public partial class ActiveReport_Variable_Height : DataDynamics.ActiveReports.ActiveReport
    {
        // 출력 인덱스
        public int RowCounter = 0;
        // 결과 데이타의 (DataRow 혹은 Class) 집합
        public object ResultData_Info = null;
        // 컬럼 헤더 정보
        public ArrayList ColumnHeader_Info = new ArrayList();
        // 진행 팝업창
        public Form Popup_ReportCreate_Progress = new Form();
        public int Popup_ReportCreate_Progress_Count = 0; // 카운트 
        // 생성자 
        public ActiveReport_Variable_Height(
            Form ProgressbarPopup,      // 진행바 팝업창
            object ResultData,          // 실제로 레포트에 출력할 데이타를 전달받음            
            ArrayList ColumnHeader,     // 컬럼 텍스트명 및 길이 추출정보필드 (참고: _Report_Column_Info 타입의 클래스의 리스트 정보)
            string Title,               // 제목
            string Summary,             // 개요
            float ColumnHeight,         // 컬럼 높이 
            int FontSize)               // 폰트 사이즈
        {
            //
            // Required for Windows Form Designer support
            //
            
            InitializeComponent();
            //
            // 이벤트
            // 
            this.DataInitialize += new System.EventHandler(this.ActiveReport_Variable_DataInitialize);
            this.FetchData += new FetchEventHandler(ActiveReport_Variable_FetchData);
            
            //
            // 데이타  (결과가 어떤 타입인지에 따라 레포트 타입이 결정된다)
            //
            ResultData_Info = ResultData;
            //
            // UI
            //
            
            // 제목
            this.label_Header_Title.Text = Title;
            
            // 개요
            this.textBox_Header_Summary.Text = Summary;
            // 헤더정보
            ColumnHeader_Info = ColumnHeader;
            // 필드 생성 (디자이너의 TextBox 마다 지정되어 있는 필드와 매칭하기 위해서는 반드시 아래와 동일한 이름으로 추가해야 한다)
            _ActiveReport.Create_Column(this, this.pageHeader, this.detail, ColumnHeader_Info, ColumnHeight, FontSize);
            // 진행팝업창
            Popup_ReportCreate_Progress = ProgressbarPopup;
        }
        /// 
		/// DataInitialize Event
		/// ActiveReports event that is called during the report initalization
		/// procedure. (after .Run is called on your report object)  Normally used
		/// with unbound reporting to establish an active connection to your data
		/// to be used with the FetchData event, or to setup a bound report with a
		/// dynamic database connection at runtime.
		/// This is also where you should include any Fields values that you need to
		/// add for unbound reporting.
		/// 
        private void ActiveReport_Variable_DataInitialize(object sender, System.EventArgs eArgs)
        {
            ;
        }
        
         /// 
        /// FetchData Event
        /// ActiveReports event that is called during the report run once per
        /// row from the dataset.  This event is usually only used in unbound reporting,
        /// you would set the Fields collection value to the value from your dataset and
        /// advance the next row.  When you run out of data, you should set the
        /// FetchEventArgs argument's EOF field to true to tell the report that the report
        /// is done with the data.
        /// 
        private void ActiveReport_Variable_FetchData(object sender, FetchEventArgs eArgs)
        {
            //
            // DataRowCollection
            //
            if (ResultData_Info.GetType() == typeof(DataRowCollection))
            {
                if (RowCounter == ((DataRowCollection)ResultData_Info).Count)
                {
                    // 데이타를 다 읽었음
                    eArgs.EOF = true;
                }
                else
                {
                    // ColumnHeader_Info 의 경우 DataRowCollection 의 DataRow 변수명이 동일하다고 가정한다
                    for (int i = 0; i < ColumnHeader_Info.Count; i++)
                    {
                        // 키값 조회
                        string Key = ((_Report_Column_Info)ColumnHeader_Info[i]).Column_Key;
                        // 예외 조건 확인후 레포트 데이타 삽입
                        if (_ActiveReport.Exception_ReportPrint(this, Key, RowCounter, ((DataRow)((DataRowCollection)ResultData_Info)[RowCounter])) == false)
                            this.Fields[Key].Value = ((DataRow)((DataRowCollection)ResultData_Info)[RowCounter])[Key].ToString();
                    }
                    // 진행바 카운터가 증가한다                      
                    _Event.ProgressPopupCount_SendMessage(Popup_ReportCreate_Progress, ++Popup_ReportCreate_Progress_Count);
                    // 인덱스 증가
                    RowCounter++;
                    // 데이타를 아직 덜읽었음
                    eArgs.EOF = false;
                }
            }
            //
            // ArrayList
            //
            else if (ResultData_Info.GetType() == typeof(ArrayList))
            {
                if (RowCounter == ((ArrayList)ResultData_Info).Count)
                {
                    // 데이타를 다 읽었음
                    eArgs.EOF = true;
                }
                else
                {
                    // ColumnHeader_Info 의 경우 ArrayList 의 클래스의 멤버명이 일치한다고 가정한다
                    for (int i = 0; i < ColumnHeader_Info.Count; i++)
                    {
                        // 키값 조회
                        string Key = ((_Report_Column_Info)ColumnHeader_Info[i]).Column_Key;
                        // 레포트 데이타 타입
                        this.Fields[Key].Value = _Reflection.GetMemberValue_ArrayList_ByClass(((ArrayList)ResultData_Info), RowCounter, Key);                        
                    }
                    // 진행바 카운터가 증가한다                      
                    _Event.ProgressPopupCount_SendMessage(Popup_ReportCreate_Progress, ++Popup_ReportCreate_Progress_Count);
                    // 인덱스 증가
                    RowCounter++;
                    // 데이타를 아직 덜읽었음
                    eArgs.EOF = false;
                }
            }
        }
        
    }
}