using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using util = FCOMMON.Util;
namespace FPJ0000
{
    public partial class fProjectIOMap : FCOMMON.fBase
    {
        string fn_fpcolsizeI = "";
        string fn_fpcolsizeO = "";
        string fn_fpcolsizeE = "";

        int projectindex = 0;
        public fProjectIOMap(int project_)
        {
            InitializeComponent();
            projectindex = project_;
            this.dsPRJ.ProjectsIOMap.TableNewRow += ProjectsSchedule_TableNewRow;
            fn_fpcolsizeI = util.MakeFilePath(util.CurrentPath, "formSetting", "fp_" + this.Name + "i.ini");
            fn_fpcolsizeO = util.MakeFilePath(util.CurrentPath, "formSetting", "fp_" + this.Name + "o.ini");
            fn_fpcolsizeE = util.MakeFilePath(util.CurrentPath, "formSetting", "fp_" + this.Name + "e.ini");
        }

        void ProjectsSchedule_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["project"] = this.projectindex;
        }

        private void fProjectSchedule_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            RefreshData();

        }
        void RefreshData()
        {
            try
            {
                this.ta.Fill(this.dsPRJ.ProjectsIOMap, this.projectindex);
                FPUtil.ColSizeLoad(ref this.fpIN, fn_fpcolsizeI);
                FPUtil.ColSizeLoad(ref this.fpOut, fn_fpcolsizeO);
                FPUtil.ColSizeLoad(ref this.fpEtc, fn_fpcolsizeE);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }


        private void autoToolStripMenuItem_Click(object sender, EventArgs e)
        {


            this.fpIN.ActiveSheet.DataAutoSizeColumns = true;

            for (int i = 0; i < this.fpIN.ActiveSheet.Rows.Count; i++)
                this.fpIN.ActiveSheet.SetRowHeight(i, 25);
            //dv1.AutoResizeColumns();
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (FarPoint.Win.Spread.Column col in this.fpIN.ActiveSheet.Columns)
            {
                col.Width = 100;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColsizeSave(this.fpIN, fn_fpcolsizeI);
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColSizeLoad(ref this.fpIN, fn_fpcolsizeI);
        }



        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bsOut.EndEdit();
            this.bsETC.EndEdit();
            this.bsIn.EndEdit();
            try
            {
                this.tam.UpdateAll(this.dsPRJ);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }

        }
        private void exportListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sd = new SaveFileDialog();
            sd.Filter = "Excel|*.xls";
            sd.FileName = "iomap_in";
            if (sd.ShowDialog() != DialogResult.OK) return;
            fpIN.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            var sd = new SaveFileDialog();
            sd.Filter = "Excel|*.xls";
            sd.FileName = "iomap_out";
            if (sd.ShowDialog() != DialogResult.OK) return;
            fpIN.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
        }

        private void toolStripMenuItem13_Click(object sender, EventArgs e)
        {
            var sd = new SaveFileDialog();
            sd.Filter = "Excel|*.xls";
            sd.FileName = "iomap_etc";
            if (sd.ShowDialog() != DialogResult.OK) return;
            fpIN.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            this.fpEtc.ActiveSheet.DataAutoSizeColumns = true;

            for (int i = 0; i < this.fpEtc.ActiveSheet.Rows.Count; i++)
                this.fpEtc.ActiveSheet.SetRowHeight(i, 25);
            //dv1.AutoResizeColumns();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {


            this.fpOut.ActiveSheet.DataAutoSizeColumns = true;

            for (int i = 0; i < this.fpOut.ActiveSheet.Rows.Count; i++)
                this.fpOut.ActiveSheet.SetRowHeight(i, 25);
            //dv1.AutoResizeColumns();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            foreach (FarPoint.Win.Spread.Column col in this.fpOut.ActiveSheet.Columns)
            {
                col.Width = 100;
            }
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            foreach (FarPoint.Win.Spread.Column col in this.fpEtc.ActiveSheet.Columns)
            {
                col.Width = 100;
            }
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            FPUtil.ColsizeSave(this.fpOut, fn_fpcolsizeO);
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            FPUtil.ColsizeSave(this.fpEtc, fn_fpcolsizeE);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            FPUtil.ColSizeLoad(ref this.fpOut, fn_fpcolsizeO);
        }

        private void toolStripMenuItem12_Click(object sender, EventArgs e)
        {
            FPUtil.ColSizeLoad(ref this.fpEtc, fn_fpcolsizeE);
        }

        private void bindingNavigatorAddNewItem1_Click(object sender, EventArgs e)
        {
            var newitem = bsIn.AddNew() as DataRowView;
            newitem["Dir"] = "IN";

        }

        private void bindingNavigatorAddNewItem2_Click(object sender, EventArgs e)
        {
            var newitem = bsOut.AddNew() as DataRowView;
            newitem["Dir"] = "OUT";
        }

        private void bindingNavigatorAddNewItem3_Click(object sender, EventArgs e)
        {
            var newitem = bsETC.AddNew() as DataRowView;
            newitem["Dir"] = "ETC";
        }

        private void DefaultMake_Click(object sender, EventArgs e)
        {
            if (this.dsPRJ.ProjectsIOMap.Rows.Count > 0)
            {
                util.MsgE("기존에 저장된 자료가 있으면 생성하지 못합니다.");
                return;
            }

            var dlg = util.MsgQ("입력 64, 출력 64개의 목록을 생성 합니다\n진행 할까요?");
            if (dlg != DialogResult.Yes) return;

            for (int i = 0; i < 64; i++)
            {
                //input
                var newdi = this.dsPRJ.ProjectsIOMap.NewProjectsIOMapRow();
                newdi.Dir = "IN";
                newdi.PIn = "X" + i.ToString("X2");
                this.dsPRJ.ProjectsIOMap.AddProjectsIOMapRow(newdi);

                //output
                var newdo = this.dsPRJ.ProjectsIOMap.NewProjectsIOMapRow();
                newdo.Dir = "OUT";
                newdo.PIn = "Y" + i.ToString("X2");
                this.dsPRJ.ProjectsIOMap.AddProjectsIOMapRow(newdo);

            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //var bs = new BindingSource(this.dsPRJ, "ProjectsIOMap");
            //bs.Sort = "Dir desc";
            //var fp = new FarPoint.Win.Spread.FpSpread();
            //var newsheet = new FarPoint.Win.Spread.SheetView();
            //newsheet.SheetName = "Sheet1";
            //newsheet.DataSource = bs;
            //fp.Sheets.Add(newsheet);
            var sd = new SaveFileDialog();
            sd.Filter = "Excel|*.xlsx";
            sd.FileName = "iomap";
            if (sd.ShowDialog() != DialogResult.OK) return;
            //var rlt = fp.SaveExcel(sd.FileName,
            //        FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
            //        | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
            //        | FarPoint.Excel.ExcelSaveFlags.NoFormulas
            //        | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
            //if (rlt == false)
            //    FCOMMON.Util.MsgE("저장 실패");



            libxl.Book book;// = new libxl.BinBook();
            book = new libxl.XmlBook();
            book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);

            var cfTitle = book.addFormat();
            cfTitle.alignH = libxl.AlignH.ALIGNH_CENTER;
            cfTitle.alignV = libxl.AlignV.ALIGNV_CENTER;
            cfTitle.setBorder(libxl.BorderStyle.BORDERSTYLE_THICK);


            var cfTitleIn = book.addFormat();
            cfTitleIn.alignH = libxl.AlignH.ALIGNH_CENTER;
            cfTitleIn.alignV = libxl.AlignV.ALIGNV_CENTER;
            cfTitleIn.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
            cfTitleIn.patternForegroundColor = libxl.Color.COLOR_YELLOW;

            var cfTitleOut = book.addFormat();
            cfTitleOut.alignH = libxl.AlignH.ALIGNH_CENTER;
            cfTitleOut.alignV = libxl.AlignV.ALIGNV_CENTER;
            cfTitleOut.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
            cfTitleOut.patternForegroundColor = libxl.Color.COLOR_PINK;

            var cfTitleEtc = book.addFormat();
            cfTitleEtc.alignH = libxl.AlignH.ALIGNH_CENTER;
            cfTitleEtc.alignV = libxl.AlignV.ALIGNV_CENTER;
            cfTitleEtc.fillPattern = libxl.FillPattern.FILLPATTERN_SOLID;
            cfTitleEtc.patternForegroundColor = libxl.Color.COLOR_TEAL_CL;


            var sheet = book.addSheet("IOMap");

            

            sheet.writeStr(0, 1, "INPUT", cfTitle);
            sheet.setMerge(0, 0, 1, 3);
            var cf = sheet.cellFormat(0, 1);
      

            sheet.writeStr(1, 1, "Pin", cfTitleIn);
            sheet.writeStr(1, 2, "Title", cfTitleIn);
            sheet.writeStr(1, 3, "Remark", cfTitleIn);


            sheet.writeStr(0, 5, "OUTPUT", cfTitle);
            sheet.setMerge(0, 0, 5, 7);


            sheet.writeStr(1, 5, "Pin", cfTitleOut);
            sheet.writeStr(1, 6, "Title", cfTitleOut);
            sheet.writeStr(1, 7, "Remark", cfTitleOut);


            sheet.writeStr(0, 9, "ETC", cfTitle);
            sheet.setMerge(0, 0, 9, 12);


            sheet.writeStr(1, 9, "Dir", cfTitleEtc);
            sheet.writeStr(1, 10, "Pin", cfTitleEtc);
            sheet.writeStr(1, 11, "Title", cfTitleEtc);
            sheet.writeStr(1, 12, "Remark", cfTitleEtc);



            var row = 2;
            for (int r = 0; r < fpIN.ActiveSheet.RowCount; r++)
            {
                var col = 1;
                var pin = fpIN.ActiveSheet.Cells[r, 0].Value?.ToString() ?? string.Empty;
                var title = fpIN.ActiveSheet.Cells[r, 1].Value?.ToString() ?? string.Empty;
                var remark = fpIN.ActiveSheet.Cells[r, 2].Value?.ToString() ?? string.Empty;

                sheet.writeStr(row, col++, pin);
                sheet.writeStr(row, col++, title);
                sheet.writeStr(row, col++, remark);

                row += 1;
            }

            row = 2;
            for (int r = 0; r < fpOut.ActiveSheet.RowCount; r++)
            {
                var col = 5;
                var pin = fpOut.ActiveSheet.Cells[r, 0].Value?.ToString() ?? string.Empty;
                var title = fpOut.ActiveSheet.Cells[r, 1].Value?.ToString() ?? string.Empty;
                var remark = fpOut.ActiveSheet.Cells[r, 2].Value?.ToString() ?? string.Empty;

                sheet.writeStr(row, col++, pin);
                sheet.writeStr(row, col++, title);
                sheet.writeStr(row, col++, remark);

                row += 1;
            }

            row = 2;
            for (int r = 0; r < fpEtc.ActiveSheet.RowCount; r++)
            {
                var col = 9;
                var dir = fpEtc.ActiveSheet.Cells[r, 0].Value?.ToString() ?? string.Empty;
                var pin = fpEtc.ActiveSheet.Cells[r, 1].Value?.ToString() ?? string.Empty;
                var title = fpEtc.ActiveSheet.Cells[r, 2].Value?.ToString() ?? string.Empty;
                var remark = fpEtc.ActiveSheet.Cells[r, 3].Value?.ToString() ?? string.Empty;

                sheet.writeStr(row, col++, dir);
                sheet.writeStr(row, col++, pin);
                sheet.writeStr(row, col++, title);
                sheet.writeStr(row, col++, remark);

                row += 1;
            }


            book.save(sd.FileName);
            util.RunExplorer(sd.FileName);



        }
    }
}
