using MMoneyCoderSharp.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MMoneyCoderSharp.DBDefine;

namespace MMoneyCoderSharp.Data.Request
{
    internal class StockYield : ADataObject
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recvStockName"></param>
        /// <param name="recvDataLength"></param>
        public StockYield(string recvStockNameKorean, 데이터기간 recvDataLength, 국내시장종류 recvMarketType)
        {
            if (recvStockNameKorean == "" || recvStockNameKorean == null)
            {
                throw new Exception("선그래프, 종목수익률, 종목명이 없음");
            }

            this.mStockNameKorean = recvStockNameKorean;
            this.mDataLength = recvDataLength;
            this.mMarketType = recvMarketType;
        }

        private string mStockNameKorean = "";

        private 데이터기간 mDataLength;

        private 국내시장종류 mMarketType;

        const string queryKOSPI = @"select O.open_day data_day , X.curr_price, X.chg_type, 
            X.rate, X.net_chg, X.f_stock_wanname 
            from(select a.f_stock_code, a.f_data_date data_day, a.f_curr_price curr_price, a.f_chg_type chg_type, a.F_net_CHG net_chg, 
                b.f_stock_wanname, 
                round((a.f_net_chg / decode(a.f_chg_type, '1', (a.f_curr_price - a.f_net_chg), 
                    '2', (a.f_curr_price - a.f_net_chg), 
                    '3', (a.f_curr_price), 
                    '4', -1*(a.f_curr_price + a.f_net_chg), 
                    '5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate 
                from t_candle_history a, t_stock b 
    where a.f_stock_code = b.f_stock_code 
    and b.f_stock_wanname = '{0}' 
        and b.f_mkt_halt = 'N') X, 
            (select open_day from(select open_day from v_open_day 
                minus 
                select substr(f_data_time, 1, 8) data_day 
                from t_index 
    where f_part_code = '001' 
        order by open_day desc) 
    where rownum< {1}) O 
    where X.data_day(+) = o.open_day 
        UNION 
        select(select substr(f_data_time, 1, 8) data_day from t_index where f_part_code = '001') data_day, 
        a.f_curr_price curr_price, a.f_chg_type chg_type, 
        round((a.f_net_chg / decode(a.f_chg_type, '1', (a.f_curr_price - a.f_net_chg), 
            '2', (a.f_curr_price - a.f_net_chg), 
            '3', (a.f_curr_price), 
            '4', -1*(a.f_curr_price + a.f_net_chg), 
            '5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate, 
        a.F_net_CHG net_chg, b.f_stock_wanname 
        from t_online1 a, t_stock b 
    where a.f_stock_code = b.f_stock_code 
    and b.f_stock_wanname = '{0}' 
        and b.f_mkt_halt = 'N' 
        order by data_day desc";

        const string queryKOSDAQ = @"select O.open_day data_day , X.curr_price, X.chg_type, 
            X.rate, X.net_chg, X.f_stock_wanname 
            from(select a.f_stock_code, a.f_data_date data_day, a.f_curr_price curr_price, a.f_chg_type chg_type, a.F_net_CHG net_chg, 
                b.f_stock_wanname, 
                round((a.f_net_chg / decode(a.f_chg_type, '1', (a.f_curr_price - a.f_net_chg), 
                    '2', (a.f_curr_price - a.f_net_chg), 
                    '3', (a.f_curr_price), 
                    '4', -1*(a.f_curr_price + a.f_net_chg), 
                    '5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate 
                from t_kosdaq_candle_history a, t_kosdaq_stock b 
    where a.f_stock_code = b.f_stock_code 
    and b.f_stock_wanname = '{0}' 
        and b.f_mkt_halt = 'N') X, 
            (select open_day from(select open_day from v_open_day 
                minus 
                select substr(f_data_time, 1, 8) data_day 
                from t_index 
    where f_part_code = '001'
        order by open_day desc) 
    where rownum < {1}) O 
    where X.data_day(+) = o.open_day 
        UNION 
        select(select substr(f_data_time, 1, 8) data_day  from t_index  where f_part_code = '001') data_day, 
        a.f_curr_price curr_price, a.f_chg_type chg_type, 
        round((a.f_net_chg / decode(a.f_chg_type, '1', (a.f_curr_price - a.f_net_chg), 
            '2', (a.f_curr_price - a.f_net_chg), 
            '3', (a.f_curr_price), 
            '4', -1*(a.f_curr_price + a.f_net_chg), 
            '5', -1*(a.f_curr_price + a.f_net_chg))) * 100, 2) rate, 
        a.F_net_CHG net_chg, b.f_stock_wanname 
        from t_kosdaq_online1 a, t_kosdaq_stock b 
    where a.f_stock_code = b.f_stock_code 
    and b.f_stock_wanname = '{0}' 
        and b.f_mkt_halt = 'N' 
        order by data_day desc";

        public override IDataRequest buildData()
        {
            string bufQuery = string.Empty;

            if (mMarketType == 국내시장종류.KOSPI)
            {
                bufQuery = String.Format(queryKOSPI, this.mStockNameKorean, this.mDataLength.GetStringValue());
            }
            else
            {
                bufQuery = String.Format(queryKOSDAQ, this.mStockNameKorean, this.mDataLength.GetStringValue());
            }

            mRequestQuery.Add("", bufQuery);

            return this;
        }
    }

}
