using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;

namespace Azyobuzi.Twirunrun
{
    public class Settings : SettingsBase<Settings>
    {
        //
        //アクセストークン
        //
        private string accessToken;
        public string AccessToken
        {
            set
            {
                if (accessToken != value)
                {
                    accessToken = value;
                    OnPropertyChanged("AccessToken");
                }
            }
            get
            {
                return accessToken;
            }
        }

        private string accessTokenSecret;
        public string AccessTokenSecret
        {
            set
            {
                if (accessTokenSecret != value)
                {
                    accessTokenSecret = value;
                    OnPropertyChanged("AccessTokenSecret");
                }
            }
            get
            {
                return accessTokenSecret;
            }
        }

        private string myScreenName;
        public string MyScreenName
        {
            set
            {
                if (myScreenName != value)
                {
                    myScreenName = value;
                    OnPropertyChanged("MyScreenName");
                }
            }
            get
            {
                return myScreenName;
            }
        }


        //
        //URI短縮
        //
        private string _bitlyId;
        public string bitlyId
        {
            set
            {
                if (_bitlyId != value)
                {
                    _bitlyId = value;
                    OnPropertyChanged("bitlyId");
                }
            }
            get
            {
                return _bitlyId;
            }
        }

        private string _bitlyApiKey;
        public string bitlyApiKey
        {
            set
            {
                if (_bitlyApiKey != value)
                {
                    _bitlyApiKey = value;
                    OnPropertyChanged("bitlyApiKey");
                }
            }
            get
            {
                return _bitlyApiKey;
            }
        }

        private string selectedShortenService = ShortenUriService.bitly.Name;
        public string SelectedShortenService
        {
            set
            {
                if (!selectedShortenService.Equals(value, StringComparison.CurrentCultureIgnoreCase))
                {
                    selectedShortenService = value;
                    OnPropertyChanged("SelectedShortenService");
                }
            }
            get
            {
                return selectedShortenService;
            }
        }

        private bool autoShorten = false;
        public bool AutoShorten
        {
            set
            {
                if (autoShorten != value)
                {
                    autoShorten = value;
                    OnPropertyChanged("AutoShorten");
                }
            }
            get
            {
                return autoShorten;
            }
        }


        //
        //ビューア設定
        //
        private FormSettings viewerForm = new FormSettings()
        #region ViewerForm初期設定
        {
            Bounds = new Rectangle(100, 100, 400, 400),
            WindowState = FormWindowState.Normal
        };
        #endregion
        public FormSettings ViewerForm
        {
            set
            {
                if (!viewerForm.Equals(value))
                {
                    viewerForm = value;
                    OnPropertyChanged("ViewerForm");
                }
            }
            get
            {
                return viewerForm;
            }
        }

        private bool pictureViewerAutoSize = true;
        public bool PictureViewerAutoSize
        {
            set
            {
                if (pictureViewerAutoSize != value)
                {
                    pictureViewerAutoSize = value;
                    OnPropertyChanged("PictureViewerAutoSize");
                }
            }
            get
            {
                return pictureViewerAutoSize;
            }
        }


        //
        //表示設定
        //
        private int iconSize = 16;
        public int IconSize
        {
            set
            {
                if (iconSize != value)
                {
                    iconSize = value;
                    OnPropertyChanged("IconSize");
                }
            }
            get
            {
                return iconSize;
            }
        }

        private string mainFont = new Font("MS UI Gothic", 9f).ConvertToString();
        public string MainFont
        {
            set
            {
                if (mainFont != value)
                {
                    mainFont = value;
                    OnPropertyChanged("MainFont");
                }
            }
            get
            {
                return mainFont;
            }
        }

        private string mainFontColor = SystemColors.ControlText.ConvertToString();
        public string MainFontColor
        {
            set
            {
                if (mainFontColor != value)
                {
                    mainFontColor = value;
                    OnPropertyChanged("MainFontColor");
                }
            }
            get
            {
                return mainFontColor;
            }
        }

        private string mainBgColor = Color.White.ConvertToString();
        public string MainBgColor
        {
            set
            {
                if (mainBgColor != value)
                {
                    mainBgColor = value;
                    OnPropertyChanged("MainBgColor");
                }
            }
            get
            {
                return mainBgColor;
            }
        }

        private string retweetedBgColor = Color.Green.ConvertToString();
        public string RetweetedBgColor
        {
            set
            {
                if (retweetedBgColor != value)
                {
                    retweetedBgColor = value;
                    OnPropertyChanged("RetweetedBgColor");
                }
            }
            get
            {
                return retweetedBgColor;
            }
        }

        private string postPanelFont = new Font("MS UI Gothic", 9f).ConvertToString();
        public string PostPanelFont
        {
            set
            {
                if (postPanelFont != value)
                {
                    postPanelFont = value;
                    OnPropertyChanged("PostPanelFont");
                }
            }
            get
            {
                return postPanelFont;
            }
        }

        private int nameWidth = 100;
        public int NameWidth
        {
            set
            {
                if (nameWidth != value)
                {
                    nameWidth = value;
                    OnPropertyChanged("NameWidth");
                }
            }
            get
            {
                return nameWidth;
            }
        }

        private NameTypes nameType = NameTypes.Name;
        public NameTypes NameType
        {
            set
            {
                if (nameType != value)
                {
                    nameType = value;
                    OnPropertyChanged("NameType");
                }
            }
            get
            {
                return nameType;
            }
        }


        //
        //フッタ
        //
        private string footer;
        public string Footer
        {
            set
            {
                if (footer != value)
                {
                    footer = value;
                    OnPropertyChanged("Footer");
                }
            }
            get
            {
                return footer;
            }
        }


        //
        //投稿キー
        //
        private PostKeys postKey = PostKeys.Enter;
        public PostKeys PostKey
        {
            set
            {
                if (postKey != value) { 
                    postKey = value;
                    OnPropertyChanged("PostKey");
                }
            }
            get
            {
                return postKey;
            }
        }


        //
        //並べ替え順
        //
        private OrderTypes orderType = OrderTypes.Descending;
        public OrderTypes OrderType
        {
            set
            {
                if (orderType != value)
                {
                    orderType = value;
                    OnPropertyChanged("OrderType");
                }
            }
            get
            {
                return orderType;
            }
        }


        //
        //投稿欄の位置
        //
        private DockStyle postPanelPosition = DockStyle.Bottom;
        public DockStyle PostPanelPosition
        {
            set
            {
                if (postPanelPosition != value)
                {
                    postPanelPosition = value;
                    OnPropertyChanged("PostPanelPosition");
                }
            }
            get
            {
                return postPanelPosition;
            }
        }


        //
        //タブの位置
        //
        private TabAlignment tabAlignment = TabAlignment.Top;
        public TabAlignment TabAlignment
        {
            set
            {
                if (tabAlignment != value)
                {
                    tabAlignment = value;
                    OnPropertyChanged("TabAlignment");
                }
            }
            get
            {
                return tabAlignment;
            }
        }


        //
        //投稿欄の高さ
        //
        private int postPanelHeight = 75;
        public int PostPanelHeight
        {
            set
            {
                if (postPanelHeight != value)
                {
                    postPanelHeight = value;
                    OnPropertyChanged("PostPanelHeight");
                }
            }
            get
            {
                return postPanelHeight;
            }
        }


        //
        //タブ
        //
        private List<TabInfo> tabs = new List<TabInfo>();
        public List<TabInfo> Tabs
        {
            set
            {
                if (!tabs.SequenceEqual(value))
                {
                    tabs = value;
                    OnPropertyChanged("Tabs");
                }
            }
            get
            {
                return tabs;
            }
        }
    }

    public class FormSettings : IEquatable<FormSettings>
    {
        public Rectangle Bounds { set; get; }
        public FormWindowState WindowState { set; get; }

        public static FormSettings FromForm(Form f)
        {
            return new FormSettings()
            {
                Bounds = f.Bounds,
                WindowState = f.WindowState
            };
        }

        public void Restore(Form f)
        {
            f.Bounds = Bounds;
            f.WindowState = WindowState;
        }

        public bool Equals(FormSettings other)
        {
            return this.Bounds == other.Bounds && this.WindowState == other.WindowState;
        }
    }

    public enum PostKeys
    {
        Enter,
        CtrlEnter,
        ShiftEnter
    }

    public enum OrderTypes
    {
        /// <summary>
        /// 昇順
        /// </summary>
        Ascending,
        /// <summary>
        /// 降順
        /// </summary>
        Descending
    }

    public enum NameTypes
    {
        Name,
        ScreenName
    }
}
