import React, { useState } from 'react';
import {
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  Button,
  Box,
  Tabs,
  Tab,
  IconButton,
  Typography,
  useTheme,
} from '@mui/material';
import { Close, ShowChart, Insights, BarChart, CandlestickChart } from '@mui/icons-material';
import MASettingsPanel from './MASettingsPanel';
import IchimokuSettingsPanel from './IchimokuSettingsPanel';
import IndicatorSettingsPanel from './IndicatorSettingsPanel';
import CandleSettingsPanel from './CandleSettingsPanel';
import DraggablePaper from './DraggablePaper';

interface ChartSettingsDialogProps {
  open: boolean;
  onClose: () => void;
  initialTab?: number;
}

interface TabPanelProps {
  children?: React.ReactNode;
  index: number;
  value: number;
}

const TabPanel: React.FC<TabPanelProps> = ({ children, value, index }) => {
  return (
    <Box
      role="tabpanel"
      hidden={value !== index}
      sx={{ 
        height: 'calc(100% - 48px)', 
        overflow: 'auto',
        p: 2,
      }}
    >
      {value === index && children}
    </Box>
  );
};

const ChartSettingsDialog: React.FC<ChartSettingsDialogProps> = ({ 
  open, 
  onClose,
  initialTab = 0,
}) => {
  const theme = useTheme();
  const [activeTab, setActiveTab] = useState(initialTab);

  const handleTabChange = (_: React.SyntheticEvent, newValue: number) => {
    setActiveTab(newValue);
  };

  return (
    <Dialog
      open={open}
      onClose={onClose}
      maxWidth="md"
      fullWidth
      hideBackdrop={true}
      disableScrollLock={true}
      PaperComponent={DraggablePaper}
      sx={{
        pointerEvents: 'none',
        '& .MuiDialog-container': {
          pointerEvents: 'none',
          alignItems: 'flex-start',
        },
      }}
      PaperProps={{
        sx: {
          height: '85vh',
          maxHeight: 850,
          bgcolor: theme.palette.mode === 'dark' ? '#1a1a2e' : '#fff',
          pointerEvents: 'auto',
          boxShadow: '0 8px 32px rgba(0, 0, 0, 0.3)',
          border: theme.palette.mode === 'dark' ? '2px solid rgba(80, 100, 160, 0.5)' : '1px solid #ddd',
        },
      }}
    >
      <DialogTitle
        sx={{
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'space-between',
          bgcolor: '#1976d2',
          color: '#fff',
          py: 1.2,
          px: 2,
        }}
      >
        <Typography variant="h6" sx={{ fontWeight: 600, fontSize: '16px', color: '#fff' }}>
          ⚙️ 차트 설정
        </Typography>
        <IconButton onClick={onClose} size="small" sx={{ color: '#fff' }}>
          <Close />
        </IconButton>
      </DialogTitle>

      <Box sx={{ borderBottom: 1, borderColor: 'divider', px: 1 }}>
        <Tabs
          value={activeTab}
          onChange={handleTabChange}
          variant="fullWidth"
          sx={{
            '& .MuiTab-root': {
              minHeight: 48,
              fontSize: '13px',
              fontWeight: 500,
            },
          }}
        >
          <Tab 
            icon={<ShowChart sx={{ fontSize: 18 }} />} 
            iconPosition="start" 
            label="이동평균선" 
          />
          <Tab 
            icon={<Insights sx={{ fontSize: 18 }} />} 
            iconPosition="start" 
            label="일목균형표" 
          />
          <Tab 
            icon={<BarChart sx={{ fontSize: 18 }} />} 
            iconPosition="start" 
            label="보조지표" 
          />
          <Tab 
            icon={<CandlestickChart sx={{ fontSize: 18 }} />} 
            iconPosition="start" 
            label="캔들차트" 
          />
        </Tabs>
      </Box>

      <DialogContent sx={{ p: 0, overflow: 'hidden', height: '100%' }}>
        <TabPanel value={activeTab} index={0}>
          <MASettingsPanel />
        </TabPanel>
        <TabPanel value={activeTab} index={1}>
          <IchimokuSettingsPanel />
        </TabPanel>
        <TabPanel value={activeTab} index={2}>
          <IndicatorSettingsPanel />
        </TabPanel>
        <TabPanel value={activeTab} index={3}>
          <CandleSettingsPanel />
        </TabPanel>
      </DialogContent>

      <DialogActions sx={{ borderTop: 1, borderColor: 'divider', px: 2, py: 1.5 }}>
        <Button onClick={onClose} variant="contained" color="primary">
          닫기
        </Button>
      </DialogActions>
    </Dialog>
  );
};

export default ChartSettingsDialog;

