import type { ChartColorSettings } from '../contexts/ChartColorContext';
import {
  defaultMultiChartBollinger,
  defaultMultiChartIchimoku,
  type MultiChartBollingerState,
  type MultiChartIchimokuState,
} from '../stores/useDashboardStore';

/** IchimokuSettingsDialog 로컬 상태와 동일 키 */
export type IchimokuDialogLocalSettings = {
  ichimokuTenkan: boolean;
  ichimokuKijun: boolean;
  ichimokuChikou: boolean;
  ichimokuSenkouA: boolean;
  ichimokuSenkouB: boolean;
  ichimokuTenkanPeriod: number;
  ichimokuKijunPeriod: number;
  ichimokuChikouPeriod: number;
  ichimokuSenkouAPeriod: number;
  ichimokuSenkouBPeriod: number;
};

export type IchimokuDialogLocalColors = {
  ichimokuTenkan: string;
  ichimokuKijun: string;
  ichimokuChikou: string;
  ichimokuSenkouA: string;
  ichimokuSenkouB: string;
  ichimokuTenkanWidth: number;
  ichimokuKijunWidth: number;
  ichimokuChikouWidth: number;
  ichimokuSenkouAWidth: number;
  ichimokuSenkouBWidth: number;
  ichimokuCloudBullish: string;
  ichimokuCloudBearish: string;
};

export function ichimokuStoreToDialogSettings(ich: MultiChartIchimokuState): IchimokuDialogLocalSettings {
  return {
    ichimokuTenkan: ich.tenkan,
    ichimokuKijun: ich.kijun,
    ichimokuChikou: ich.chikou,
    ichimokuSenkouA: ich.senkouA,
    ichimokuSenkouB: ich.senkouB,
    ichimokuTenkanPeriod: ich.tenkanPeriod,
    ichimokuKijunPeriod: ich.kijunPeriod,
    ichimokuChikouPeriod: ich.chikouPeriod,
    ichimokuSenkouAPeriod: ich.senkouAPeriod,
    ichimokuSenkouBPeriod: ich.senkouBPeriod,
  };
}

export function ichimokuStoreToDialogColors(ich: MultiChartIchimokuState): IchimokuDialogLocalColors {
  return {
    ichimokuTenkan: ich.tenkanColor,
    ichimokuKijun: ich.kijunColor,
    ichimokuChikou: ich.chikouColor,
    ichimokuSenkouA: ich.senkouAColor,
    ichimokuSenkouB: ich.senkouBColor,
    ichimokuTenkanWidth: ich.tenkanWidth,
    ichimokuKijunWidth: ich.kijunWidth,
    ichimokuChikouWidth: ich.chikouWidth,
    ichimokuSenkouAWidth: ich.senkouAWidth,
    ichimokuSenkouBWidth: ich.senkouBWidth,
    ichimokuCloudBullish: ich.cloudBullish,
    ichimokuCloudBearish: ich.cloudBearish,
  };
}

export function ichimokuDialogToStore(
  localSettings: IchimokuDialogLocalSettings,
  localColors: IchimokuDialogLocalColors
): MultiChartIchimokuState {
  return {
    tenkan: localSettings.ichimokuTenkan,
    kijun: localSettings.ichimokuKijun,
    chikou: localSettings.ichimokuChikou,
    senkouA: localSettings.ichimokuSenkouA,
    senkouB: localSettings.ichimokuSenkouB,
    tenkanPeriod: localSettings.ichimokuTenkanPeriod,
    kijunPeriod: localSettings.ichimokuKijunPeriod,
    chikouPeriod: localSettings.ichimokuChikouPeriod,
    senkouAPeriod: localSettings.ichimokuSenkouAPeriod,
    senkouBPeriod: localSettings.ichimokuSenkouBPeriod,
    tenkanColor: localColors.ichimokuTenkan,
    kijunColor: localColors.ichimokuKijun,
    chikouColor: localColors.ichimokuChikou,
    senkouAColor: localColors.ichimokuSenkouA,
    senkouBColor: localColors.ichimokuSenkouB,
    tenkanWidth: localColors.ichimokuTenkanWidth,
    kijunWidth: localColors.ichimokuKijunWidth,
    chikouWidth: localColors.ichimokuChikouWidth,
    senkouAWidth: localColors.ichimokuSenkouAWidth,
    senkouBWidth: localColors.ichimokuSenkouBWidth,
    cloudBullish: localColors.ichimokuCloudBullish,
    cloudBearish: localColors.ichimokuCloudBearish,
  };
}

export function ichimokuDialogDefaultsFromStore(): {
  localSettings: IchimokuDialogLocalSettings;
  localColors: IchimokuDialogLocalColors;
} {
  const d = defaultMultiChartIchimoku();
  return {
    localSettings: ichimokuStoreToDialogSettings(d),
    localColors: ichimokuStoreToDialogColors(d),
  };
}

export type BollingerDialogLocalSettings = {
  bollingerUpper: boolean;
  bollingerMiddle: boolean;
  bollingerLower: boolean;
  bollingerPeriod: number;
  bollingerStdDev: number;
};

export type BollingerDialogLocalColors = {
  bollingerUpperColor: string;
  bollingerMiddleColor: string;
  bollingerLowerColor: string;
  bollingerUpperWidth: number;
  bollingerMiddleWidth: number;
  bollingerLowerWidth: number;
  bollingerUpperLineStyle: 'solid' | 'dashed' | 'dotted';
  bollingerMiddleLineStyle: 'solid' | 'dashed' | 'dotted';
  bollingerLowerLineStyle: 'solid' | 'dashed' | 'dotted';
  bollingerBandBackgroundColor: string;
};

export function bollingerStoreToDialogSettings(b: MultiChartBollingerState): BollingerDialogLocalSettings {
  return {
    bollingerUpper: b.upper,
    bollingerMiddle: b.middle,
    bollingerLower: b.lower,
    bollingerPeriod: b.period,
    bollingerStdDev: b.stdDev,
  };
}

export function bollingerStoreToDialogColors(b: MultiChartBollingerState): BollingerDialogLocalColors {
  return {
    bollingerUpperColor: b.upperColor,
    bollingerMiddleColor: b.middleColor,
    bollingerLowerColor: b.lowerColor,
    bollingerUpperWidth: b.upperWidth,
    bollingerMiddleWidth: b.middleWidth,
    bollingerLowerWidth: b.lowerWidth,
    bollingerUpperLineStyle: b.upperLineStyle,
    bollingerMiddleLineStyle: b.middleLineStyle,
    bollingerLowerLineStyle: b.lowerLineStyle,
    bollingerBandBackgroundColor: b.bandBackgroundColor,
  };
}

export function bollingerDialogToStore(
  localSettings: BollingerDialogLocalSettings,
  localColors: BollingerDialogLocalColors
): MultiChartBollingerState {
  return {
    upper: localSettings.bollingerUpper,
    middle: localSettings.bollingerMiddle,
    lower: localSettings.bollingerLower,
    period: localSettings.bollingerPeriod,
    stdDev: localSettings.bollingerStdDev,
    upperColor: localColors.bollingerUpperColor,
    middleColor: localColors.bollingerMiddleColor,
    lowerColor: localColors.bollingerLowerColor,
    upperWidth: localColors.bollingerUpperWidth,
    middleWidth: localColors.bollingerMiddleWidth,
    lowerWidth: localColors.bollingerLowerWidth,
    upperLineStyle: localColors.bollingerUpperLineStyle,
    middleLineStyle: localColors.bollingerMiddleLineStyle,
    lowerLineStyle: localColors.bollingerLowerLineStyle,
    bandBackgroundColor: localColors.bollingerBandBackgroundColor,
  };
}

export function bollingerDialogDefaultsFromStore(): {
  localSettings: BollingerDialogLocalSettings;
  localColors: BollingerDialogLocalColors;
} {
  const d = defaultMultiChartBollinger();
  return {
    localSettings: bollingerStoreToDialogSettings(d),
    localColors: bollingerStoreToDialogColors(d),
  };
}

/** CandlestickChart / ChartItemInvestment ichimokuSettings 형태 */
export interface IchimokuChartSettings {
  tenkan: boolean;
  kijun: boolean;
  chikou: boolean;
  senkouA: boolean;
  senkouB: boolean;
  tenkanPeriod: number;
  kijunPeriod: number;
  senkouBPeriod: number;
}

export function ichimokuStoreToChartSettings(ich: MultiChartIchimokuState): IchimokuChartSettings | undefined {
  if (!ich.tenkan && !ich.kijun && !ich.chikou && !ich.senkouA && !ich.senkouB) return undefined;
  return {
    tenkan: ich.tenkan,
    kijun: ich.kijun,
    chikou: ich.chikou,
    senkouA: ich.senkouA,
    senkouB: ich.senkouB,
    tenkanPeriod: ich.tenkanPeriod,
    kijunPeriod: ich.kijunPeriod,
    senkouBPeriod: ich.senkouBPeriod,
  };
}

/** 일목 색·굵기·구름 → CandlestickChart colorSettings 일부만 덮어씀 */
export function ichimokuStoreToColorOverrides(ich: MultiChartIchimokuState): Partial<ChartColorSettings> {
  return {
    ichimokuTenkan: ich.tenkanColor,
    ichimokuKijun: ich.kijunColor,
    ichimokuChikou: ich.chikouColor,
    ichimokuSenkouA: ich.senkouAColor,
    ichimokuSenkouB: ich.senkouBColor,
    ichimokuTenkanWidth: ich.tenkanWidth,
    ichimokuKijunWidth: ich.kijunWidth,
    ichimokuChikouWidth: ich.chikouWidth,
    ichimokuSenkouAWidth: ich.senkouAWidth,
    ichimokuSenkouBWidth: ich.senkouBWidth,
    ichimokuCloudBullish: ich.cloudBullish,
    ichimokuCloudBearish: ich.cloudBearish,
  };
}

export function bollingerStoreToColorOverrides(b: MultiChartBollingerState): Partial<ChartColorSettings> {
  return {
    bollingerUpperColor: b.upperColor,
    bollingerMiddleColor: b.middleColor,
    bollingerLowerColor: b.lowerColor,
    bollingerUpperWidth: b.upperWidth,
    bollingerMiddleWidth: b.middleWidth,
    bollingerLowerWidth: b.lowerWidth,
    bollingerUpperLineStyle: b.upperLineStyle,
    bollingerMiddleLineStyle: b.middleLineStyle,
    bollingerLowerLineStyle: b.lowerLineStyle,
    bollingerBandBackgroundColor: b.bandBackgroundColor,
  };
}
