package com.uvigo.mhp.looks;

import java.awt.*;
import java.awt.image.*;
import java.beans.*;
import org.havi.ui.*;
import org.dvb.ui.*;

// Documentar lo de widgetChanged!!!
public class HRangeLook2 extends HRangeLook
{
    private int preferredWidth, preferredHeight;
    private int thumbDegrees;
    private int pos;
    private PropertyChangeSupport changes;
    
    public HRangeLook2()
    {
        preferredWidth=preferredHeight=20;
        thumbDegrees=10;
        changes=new PropertyChangeSupport(this);
    }
    
    public synchronized Dimension getPreferredSize(HVisible visible)
    {
        return new Dimension(preferredWidth, preferredHeight);
    }
    
    public synchronized Dimension getMinimumSize(HVisible visible)
    {
        return new Dimension(preferredWidth, preferredHeight);
    }
    
    public int getPreferredWidth()
    {
        return preferredWidth;
    }
    
    public void setPreferredWidth(int pw)
    {
        if (pw<=0) return;
        int old=preferredWidth;
        preferredWidth=pw;
        if (changes!=null)
            changes.firePropertyChange("preferredWidth", old, pw);
    }
    
    public int getPreferredHeight()
    {
        return preferredHeight;
    }
    
    public void setPreferredHeight(int ph)
    {
        if (ph<=0) return;
        int old=preferredHeight;
        preferredHeight=ph;
        if (changes!=null)
            changes.firePropertyChange("preferredHeight", old, ph);
    }
    
    public int getThumbDegrees()
    {
        return thumbDegrees;
    }
    
    public void setThumbDegrees(int td)
    {
        if (td<=0) return;
        int old=thumbDegrees;
        thumbDegrees=td;
        if (changes!=null)
            changes.firePropertyChange("thumbDegrees", old, td);
    }
    
    public int hitTest(HAdjustmentValue component, Point pt)
    {
        if (!(component instanceof HStaticRange))
            return HAdjustableLook.ADJUST_NONE;
        
        // Si pulsa fuera del circulo, NONE!
        
        HStaticRange hsr=(HStaticRange) component;
        Dimension dim=hsr.getSize();
        int x=pt.x-dim.width/2;
        int y=dim.height/2-pt.y;
        x=(int) (((float) x)*((float) dim.height/(float) dim.width));
        
        int theta_click=(int) Math.toDegrees(Math.atan2(y,x));
        if (theta_click<0) theta_click=180+(180+theta_click);
        
        int min=hsr.getMinValue();
        int max=hsr.getMaxValue();
        int cur=hsr.getValue();
        int theta_val=(int) (((double) (cur-min))/((double) (max-min))*(360-thumbDegrees));
        
        if (theta_click<theta_val)
        {
            //            System.out.println("LESS");
            return HAdjustableLook.ADJUST_PAGE_LESS;
        }
        if (theta_click<=theta_val+thumbDegrees)
        {
            //            System.out.println("THUMB");
            return HAdjustableLook.ADJUST_THUMB;
        }
        //        System.out.println("MORE");
        return HAdjustableLook.ADJUST_PAGE_MORE;
    }
    
    public void widgetChanged(HVisible visible, HChangeData[] changes)
    {
        if (!(visible instanceof HStaticRange)) return;
        
        HStaticRange hsr=(HStaticRange) visible;
        Dimension dim=hsr.getSize();
        int min=hsr.getMinValue();
        int max=hsr.getMaxValue();
        int val=min;
        
        if (changes==null) return;
        for (int i=0;i<changes.length;i++)
        {
            HChangeData change=changes[i];
            switch (change.hint)
            {
                case 1:
                    Point pt=(Point) change.data;
                    int x=pt.x-dim.width/2;
                    int y=dim.height/2-pt.y;
                    x=(int) (((float) x)*((float) dim.height/(float) dim.width));
                    int theta_click=(int) Math.toDegrees(Math.atan2(y,x));
                    if (theta_click<0) theta_click=180+(180+theta_click);
                    
                    if (theta_click>(360-thumbDegrees))
                    {
                        //hsr.setValue(max);
                        return;
                    }
                    val+=Math.floor(((double) theta_click)/((double) (360-thumbDegrees))*(max-min));
                    //System.out.println("theta_click="+theta_click+",val="+val);
                    hsr.setValue(val);
                default:
                    visible.repaint();
                    break;
            }
        }
    }
    
    public void showLook(Graphics g, HVisible visible, int state)
    {
        if (!(visible instanceof HStaticRange)) return;
        Dimension dim = visible.getSize();
        
        HStaticRange hr;
        hr=(HStaticRange)visible;
        
        if (hr.getBackgroundMode()==HVisible.BACKGROUND_FILL)
        {
            g.setColor(visible.getBackground());
            g.fill3DRect(0,0,dim.width,dim.height,true);
        }
        g.setColor(visible.getForeground().darker());
        
        int min=hr.getMinValue();
        int max=hr.getMaxValue();
        int cur=hr.getValue();
        int theta_val=(int) (((double) (cur-min))/((double) (max-min))*(360-thumbDegrees));
        
        g.setColor(hr.getBackground());
        g.fillOval(0,0,dim.width,dim.height);
        g.setColor(hr.getBackground().brighter());
        g.fillOval(dim.width/16,dim.height/16, dim.width-dim.width/8,dim.height-dim.height/8);
        g.setColor(hr.getForeground());
        g.fillArc(dim.width/16,dim.height/16, dim.width-dim.width/8,dim.height-dim.height/8,theta_val,thumbDegrees);
    }
}