using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace QuestionSwapDoc.SwapWordModel
{
    /// <summary>
    /// 题型
    /// </summary>
    public class SwapTiXin
    {
        /// <summary>
        /// 选项的支持的字母
        /// </summary>
        public readonly char[] XuanXianZhiMu = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' };
        /// <summary>
        /// 判断的支持的字符
        /// </summary>
        public readonly char[] PanDuanZhiFu = new char[] { '√', '×' };
        /// <summary>
        /// 编号文本（由方法生成的编号如：一、二、三、）
        /// </summary>
        public string NumberStr { get; set; } = null;



        /// <summary>
        /// 题型
        /// </summary>
        public string TiXinStr { get; set; }
        /// <summary>
        /// 是否为选择题题型（包括单选，多选）
        /// </summary>
        public bool IsXuanZhe { get; set; } = false;
        /// <summary>
        /// 是否为判断题题型
        /// </summary>
        public bool IsPanDuan { get; set; } = false;
        /// <summary>
        /// 此题型和题型下面的题在答题卡中的表现形式
        /// </summary>
        public SwapTiXinDaTiKaType DaTiKaType { get; set; } = SwapTiXinDaTiKaType.自动;
        /// <summary>
        /// 试题集合
        /// </summary>
        public List<SwapQue> QueList { get; set; } = new List<SwapQue>();



        /// <summary>
        /// 得到答题卡（机读卡）的客观题的填涂区域
        /// </summary>
        /// <param name="isErect">是否竖着排版</param>
        /// <returns></returns>
        public List<string> GetDaTiKaJiDuKeGuanStr(bool isErect)
        {
            if (!IsXuanZhe && !IsPanDuan)
                return new List<string>();

            List<string> strR = new List<string>(QueList.Count);
            string jg = isErect ? "\r\n" : " ";

            foreach (var item in QueList)
            {
                var num = item.NumberStr.Substring(0, item.NumberStr.Length - 1);
                if (!isErect && num.Length == 1)
                    num = " " + num;

                var nums = new List<string>() { num };
                var xztc = item.XuanZheTiCount <= 0 ? 4 : item.XuanZheTiCount;
                var xx = IsXuanZhe ? XuanXianZhiMu.Take(xztc) : PanDuanZhiFu;
                foreach (var item2 in xx)
                {
                    if (IsPanDuan)
                        nums.Add($"[{item2}]");
                    else
                        nums.Add($"[ {item2} ]");
                }
                strR.Add(string.Join(jg, nums));
            }
            return strR;
        }

        /// <summary>
        /// 得到答题卡（人阅卡）的客观题的填写区域
        /// </summary>
        /// <returns></returns>
        public List<string> GetDaTiKaRenYueKeGuanStr()
        {
            if (!IsXuanZhe && !IsPanDuan)
                return new List<string>();

            List<string> strR = new List<string>(QueList.Count);
            foreach (var item in QueList)
            {
                var num = item.NumberStr.Substring(0, item.NumberStr.Length - 1);
                if (num.Length == 1)
                    num = " " + num;

                strR.Add($"{num}（     ）");
            }
            return strR;
        }

        /// <summary>
        /// 得到题型上的分的文本（仅限于下面的所有题分值一样的情况）
        /// </summary>
        /// <param name="isShow">是否显示此分（为false返回空字符串）</param>
        /// <returns></returns>
        public string GetFenStr(bool isShow = true)
        {
            if (!isShow || QueList == null || !QueList.Any() || QueList.Any(o => !o.Fen.HasValue || o.Fen <= 0))
                return string.Empty;

            var fens = QueList.Select(o => o.Fen).Distinct();
            if (fens.Count() > 1)
                return string.Empty;
            else
            {
                if (QueList.Count == 1)
                    return $"（共{fens.FirstOrDefault().Value}分）";
                else
                    return $"（每题{fens.FirstOrDefault().Value}分，共{QueList.Sum(o => o.Fen).Value}分）";
            }
        }
    }
}
