package cn.tee3.avd;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import cn.tee3.R;

public class WhiteboardToolView extends LinearLayout implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    public LinearLayout ll_draw_tool;
    private ImageView iv_fountain_pen;
    private LinearLayout ll_fountain_pen;
    private ImageView iv_fluorescent_pen;
    private LinearLayout ll_fluorescent_pen;
    private ImageView iv_color;
    private LinearLayout ll_color;
    public LinearLayout ll_set_color;
    private SeekBar sb_line_size;
    private TextView tv_line_size;
    private ImageView iv_rubber;
    private LinearLayout ll_rubber;

    private LinearLayout ll_arrow;
    private ImageView iv_arrow;
    public LinearLayout ll_laser_pen;
    private ImageView iv_laser_pen;

    private TextView tv_fountain_pen;
    private TextView tv_fluorescent_pen;
    private TextView tv_color;
    private TextView tv_arrow;
    private TextView tv_laser_pen;
    private TextView tv_rubber;

    private ImageView iv_line_color_red;
    private ImageView iv_line_color_pink;
    private ImageView iv_line_color_yellow;
    private ImageView iv_line_color_green;
    private ImageView iv_line_color_light_blue;
    private ImageView iv_line_color_blue;
    private ImageView iv_line_color_purple;

    public Context context;
    private View view;
    public CheckBox rb_draw_tool;

    private boolean isColorToolbarLongTimeNoTouch = true;
    private Handler colorToolbarHandler = new Handler();

    public boolean isDrawToolViewVisible = true;
    public boolean isClose_ColorToolbarHandler = true;
    private int lineWidth;  // 线宽3dp
    private int lineColor;

    private WhiteboardDrawView.ToolType toolType = WhiteboardDrawView.ToolType.POLY_LINE;

    public WhiteboardToolView(Context context) {
        super(context);
        this.context = context;
        viewInit(context);
        dataInit();
    }

    public WhiteboardToolView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        viewInit(context);
        dataInit();
    }

    public WhiteboardToolView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void viewInit(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.cn_tee3_draw_tool_view, this, true);

        ll_draw_tool = (LinearLayout) view.findViewById(R.id.ll_draw_tool);
        iv_fountain_pen = (ImageView) view.findViewById(R.id.iv_fountain_pen);
        iv_fluorescent_pen = (ImageView) view.findViewById(R.id.iv_fluorescent_pen);
        ll_fountain_pen = (LinearLayout) view.findViewById(R.id.ll_fountain_pen);
        ll_fluorescent_pen = (LinearLayout) view.findViewById(R.id.ll_fluorescent_pen);
        iv_color = (ImageView) view.findViewById(R.id.iv_color);
        ll_color = (LinearLayout) view.findViewById(R.id.ll_color);
        iv_rubber = (ImageView) view.findViewById(R.id.iv_rubber);
        ll_rubber = (LinearLayout) view.findViewById(R.id.ll_rubber);
        ll_set_color = (LinearLayout) view.findViewById(R.id.ll_set_color);
        sb_line_size = (SeekBar) view.findViewById(R.id.sb_line_size);
        tv_line_size = (TextView) view.findViewById(R.id.tv_line_size);

        ll_arrow = (LinearLayout) view.findViewById(R.id.ll_arrow);
        iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
        ll_laser_pen = (LinearLayout) view.findViewById(R.id.ll_laser_pen);
        iv_laser_pen = (ImageView) view.findViewById(R.id.iv_laser_pen);

        tv_fountain_pen     = (TextView) view.findViewById(R.id.tv_fountain_pen);
        tv_fluorescent_pen = (TextView) view.findViewById(R.id.tv_fluorescent_pen);
        tv_color            = (TextView) view.findViewById(R.id.tv_color);
        tv_arrow            = (TextView) view.findViewById(R.id.tv_arrow);
        tv_rubber           = (TextView) view.findViewById(R.id.tv_rubber);
        tv_laser_pen        = (TextView) view.findViewById(R.id.tv_laser_pen);

        iv_line_color_red = (ImageView) view.findViewById(R.id.iv_line_color_red);
        iv_line_color_pink = (ImageView) view.findViewById(R.id.iv_line_color_pink);
        iv_line_color_yellow = (ImageView) view.findViewById(R.id.iv_line_color_yellow);
        iv_line_color_green = (ImageView) view.findViewById(R.id.iv_line_color_green);
        iv_line_color_light_blue = (ImageView) view.findViewById(R.id.iv_line_color_light_blue);
        iv_line_color_blue = (ImageView) view.findViewById(R.id.iv_line_color_blue);
        iv_line_color_purple = (ImageView) view.findViewById(R.id.iv_line_color_purple);

        rb_draw_tool = (CheckBox) view.findViewById(R.id.rb_draw_tool);

        rb_draw_tool.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChicked) {
                if (isChicked) {
                    ll_draw_tool.setVisibility(VISIBLE);
                    isDrawToolViewVisible = true;
                    isClose_ColorToolbarHandler = true;
                } else {
                    isClose_ColorToolbarHandler = true;
                    ll_set_color.setVisibility(GONE);
                    ll_draw_tool.setVisibility(GONE);
                    isDrawToolViewVisible = false;
                }

                if (drawToolbarListener != null) {
                    drawToolbarListener.isDrawToolbarVisible(isChicked);
                }
            }
        });

//        cn_tee3_iv_draw_tool_hint.setOnClickListener(this);

        ll_fountain_pen.setOnClickListener(this);
        ll_fluorescent_pen.setOnClickListener(this);
        ll_color.setOnClickListener(this);
        ll_arrow.setOnClickListener(this);
        ll_laser_pen.setOnClickListener(this);
        ll_rubber.setOnClickListener(this);

        iv_line_color_red.setOnClickListener(this);
        iv_line_color_pink.setOnClickListener(this);
        iv_line_color_yellow.setOnClickListener(this);
        iv_line_color_green.setOnClickListener(this);
        iv_line_color_light_blue.setOnClickListener(this);
        iv_line_color_blue.setOnClickListener(this);
        iv_line_color_purple.setOnClickListener(this);
    }

    private void dataInit() {
        lineWidth = 3;
        lineColor = getResources().getColor(R.color.cn_tee3_red);

        //默认钢笔
        setTool(WhiteboardDrawView.ToolType.POLY_LINE);
        iv_color.setBackgroundColor(getResources().getColor(R.color.cn_tee3_red));
        // seekBar 配置
        sb_line_size.setMax(10);    // sp
        sb_line_size.setProgress(lineWidth);
        sb_line_size.setOnSeekBarChangeListener(this);
        tv_line_size.setText(getContext().getResources().getString(R.string.line_width) + "(" + lineWidth + "px)");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_fountain_pen) {
            setTool(WhiteboardDrawView.ToolType.POLY_LINE);
        } else if (id == R.id.ll_fluorescent_pen) {
            setTool(WhiteboardDrawView.ToolType.FLUORESCENT_PEN);
        } else if (id == R.id.ll_color) {
            colorToolbarTouchEvent();
        } else if (id == R.id.ll_arrow) {
            setTool(WhiteboardDrawView.ToolType.LOCAL_ARROW);
        } else if (id == R.id.ll_laser_pen) {
            setTool(WhiteboardDrawView.ToolType.LASER_PEN);
        } else if (id == R.id.ll_rubber) {
            setTool(WhiteboardDrawView.ToolType.RUBBER_LINE);
        } else if (id == R.id.iv_line_color_red) {
            updateColorView(R.color.cn_tee3_red);
        } else if (id == R.id.iv_line_color_pink) {
            updateColorView(R.color.cn_tee3_pink);
        } else if (id == R.id.iv_line_color_yellow) {
            updateColorView(R.color.cn_tee3_yellow);
        } else if (id == R.id.iv_line_color_green) {
            updateColorView(R.color.cn_tee3_green);
        } else if (id == R.id.iv_line_color_light_blue) {
            updateColorView(R.color.cn_tee3_light_blue);
        } else if (id == R.id.iv_line_color_blue) {
            updateColorView(R.color.cn_tee3_blue);
        } else if (id == R.id.iv_line_color_purple) {
            updateColorView(R.color.cn_tee3_purple);
        }
    }

    private void colorToolbarTouchEvent() {
        if (ll_set_color.getVisibility() == View.VISIBLE) {
            ll_set_color.setVisibility(View.GONE);
            isColorToolbarLongTimeNoTouch = true;
            colorToolbarHandler.removeCallbacks(null);
            isClose_ColorToolbarHandler = true;
        } else {
            ll_set_color.setVisibility(View.VISIBLE);
            colorToolbarHandler.removeCallbacks(null);
            isColorToolbarLongTimeNoTouch = true;
            isClose_ColorToolbarHandler = false;
            colorToolbarHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!isClose_ColorToolbarHandler) {
                        if (isColorToolbarLongTimeNoTouch) {
                            ll_set_color.setVisibility(View.GONE);
                        } else {
                            isColorToolbarLongTimeNoTouch = true;
                            colorToolbarHandler.postDelayed(this, 3000);
                        }
                    }
                }
            }, 3000);
        }
    }

    private void updateColorView(int colorId) {
        int color = getResources().getColor(colorId);
        if (toolType == WhiteboardDrawView.ToolType.FLUORESCENT_PEN) {/** 现在为非荧光笔，则将颜色半透明去除掉 */
            lineColor = Color.argb(127, Color.red(color), Color.green(color), Color.blue(color));
        } else {
            lineColor = color;
        }
        iv_color.setBackgroundColor(color);
        isColorToolbarLongTimeNoTouch = false;

        if (drawToolbarListener != null) {
            drawToolbarListener.onToolbarListener(new DrawToolbarObj(toolType, lineColor, lineWidth));
        }
    }

    private void setTool(WhiteboardDrawView.ToolType type) {
        iv_fountain_pen.setBackgroundResource(R.drawable.cn_tee3_iv_fountain_pen_no_select);
        iv_fluorescent_pen.setBackgroundResource(R.drawable.cn_tee3_iv_fluorescent_pen_no_select);
        iv_arrow.setBackgroundResource(R.drawable.cn_tee3_iv_arrow_no_select);
        iv_rubber.setBackgroundResource(R.drawable.cn_tee3_iv_rubber_no_select);
        iv_laser_pen.setBackgroundResource(R.drawable.cn_tee3_iv_laser_pen_no_select);
        tv_fountain_pen   .setTextColor(getResources().getColor(R.color.cn_tee3_White));
        tv_fluorescent_pen.setTextColor(getResources().getColor(R.color.cn_tee3_White));
        tv_color          .setTextColor(getResources().getColor(R.color.cn_tee3_White));
        tv_arrow          .setTextColor(getResources().getColor(R.color.cn_tee3_White));
        tv_rubber         .setTextColor(getResources().getColor(R.color.cn_tee3_White));
        tv_laser_pen      .setTextColor(getResources().getColor(R.color.cn_tee3_White));

        if (type != WhiteboardDrawView.ToolType.FLUORESCENT_PEN) {/** 现在为非荧光笔，则将颜色半透明去除掉 */
            lineColor = Color.argb(255, Color.red(lineColor), Color.green(lineColor), Color.blue(lineColor));
        }

        switch (type) {
            case POLY_LINE:
                iv_fountain_pen.setBackgroundResource(R.drawable.cn_tee3_iv_fountain_pen);
                tv_fountain_pen   .setTextColor(getResources().getColor(R.color.cn_tee3_tool_text_blue));
                break;
            case FLUORESCENT_PEN:
                iv_fluorescent_pen.setBackgroundResource(R.drawable.cn_tee3_iv_fluorescent_pen);
                tv_fluorescent_pen.setTextColor(getResources().getColor(R.color.cn_tee3_tool_text_blue));
                lineColor = Color.argb(127, Color.red(lineColor), Color.green(lineColor), Color.blue(lineColor)); /** 荧光笔，设置颜色半透明 */
                break;
            case LOCAL_ARROW:
                iv_arrow.setBackgroundResource(R.drawable.cn_tee3_iv_arrow);
                tv_arrow.setTextColor(getResources().getColor(R.color.cn_tee3_tool_text_blue));
                break;
            case RUBBER_LINE:
                iv_rubber.setBackgroundResource(R.drawable.cn_tee3_iv_rubber);
                tv_rubber.setTextColor(getResources().getColor(R.color.cn_tee3_tool_text_blue));
                break;
            case LASER_PEN:
                iv_laser_pen.setBackgroundResource(R.drawable.cn_tee3_iv_laser_pen);
                tv_laser_pen.setTextColor(getResources().getColor(R.color.cn_tee3_tool_text_blue));
                break;
            default:
                break;
        }

        toolType = type;
        if (drawToolbarListener != null) {
            drawToolbarListener.onToolbarListener(new DrawToolbarObj(toolType, lineColor, lineWidth));
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        isColorToolbarLongTimeNoTouch = false;
        lineWidth = progress;
        if (lineWidth == 0) {
            lineWidth = 1;
        }
        tv_line_size.setText(getContext().getResources().getString(R.string.line_width) + "(" + progress + "px)");
        if (drawToolbarListener != null) {
            drawToolbarListener.onToolbarListener(new DrawToolbarObj(toolType, lineColor, lineWidth));
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    public void setDrawToolState(boolean isChecked) {
        rb_draw_tool.setChecked(isChecked);
    }

    public class DrawToolbarObj {
        private WhiteboardDrawView.ToolType toolType;
        private int lineColor;
        private int lineWidth;

        public DrawToolbarObj(WhiteboardDrawView.ToolType toolType) {
            this.toolType = toolType;
        }

        public DrawToolbarObj(WhiteboardDrawView.ToolType toolType, int lineColor, int lineWidth) {
            this.toolType = toolType;
            this.lineColor = lineColor;
            this.lineWidth = lineWidth;
        }

        public WhiteboardDrawView.ToolType getToolType() {
            return toolType;
        }

        public void setToolType(WhiteboardDrawView.ToolType toolType) {
            this.toolType = toolType;
        }

        public int getLineColor() {
            return lineColor;
        }

        public void setLineColor(int lineColor) {
            this.lineColor = lineColor;
        }

        public int getLineWidth() {
            return lineWidth;
        }

        public void setLineWidth(int lineWidth) {
            this.lineWidth = lineWidth;
        }

        @Override
        public String toString() {
            return "DrawToolbarObj{" +
                    "toolType=" + toolType +
                    ", lineColor=" + lineColor +
                    ", lineWidth=" + lineWidth +
                    '}';
        }
    }

    public DrawToolbarListener drawToolbarListener;

    public void setDrawToolViewVisibleListener(DrawToolbarListener drawToolbarListener) {
        this.drawToolbarListener = drawToolbarListener;
    }

    public interface DrawToolbarListener {
        void onToolbarListener(DrawToolbarObj bean);
        void isDrawToolbarVisible(boolean isVisible);
    }
}
