package com.dahuo.android.cinema;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.dahuo.android.cinema.model.SeatMo;

/**
 * @author captain_miao
 */
public class SeatTableView extends View {
    Bitmap seat_sale, seat_sold, seat_selected;
    Bitmap SeatSale, SeatSold, SeatSelected;

    private int seatWidth;//座位图的宽,长宽相同
    private int defWidth;//初始值

    //放大率和移动位置
    public float mScaleFactor = 1.f;
    public float mPosX = .0f;
    public float mPosY = .0f;


    private SeatMo[][] seatTable;
    private int rowSize;
    private int columnSize;

    private Paint linePaint;//中央线的绘制

    int width;
    int height;

    public SeatTableView(Context context) {
        super(context, null);

    }

    public SeatTableView(Context context, AttributeSet attr) {
        super(context, attr);

        SeatSale = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.seat_sale);
        SeatSold = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.seat_sold);
        SeatSelected = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.seat_selected);


        setVerticalScrollBarEnabled(true);
        setHorizontalScrollBarEnabled(true);

    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //只绘制可见的座位图
        if(defWidth < 10) {
            throw new IllegalArgumentException("the width must > 10, the value is " + defWidth);
        }
        seatWidth = (int) (defWidth * mScaleFactor);
        width = getMeasuredWidth();
        height = getMeasuredHeight();


        // 可购买座位
        seat_sale = Bitmap.createScaledBitmap(SeatSale, seatWidth, seatWidth, true);
        // 红色 已售座位
        seat_sold = Bitmap.createScaledBitmap(SeatSold, seatWidth, seatWidth, true);
        // 绿色 我的选择
        seat_selected = Bitmap.createScaledBitmap(SeatSelected, seatWidth, seatWidth, true);

        // 画座位
        for (int i = 0; i < rowSize; i++) {
            //屏幕外,完整的行都不绘制
            if (i * (seatWidth) + mPosY < -seatWidth || i * (seatWidth) + mPosY > height+seatWidth) {
                continue;
            }
            //绘制中线,座位间隔由图片来做,简化处理
            if (linePaint != null) {
                canvas.drawLine((columnSize * seatWidth) / 2 + mPosX, i * (seatWidth) + mPosY,
                        (columnSize * seatWidth) / 2 + mPosX, i * (seatWidth) + seatWidth + mPosY, linePaint);
            }
            int k = (int)(mPosX + seatWidth + 0.5f);
            k = k > 0 ? 0 : -k / seatWidth;//移动距离不可能出现移到-rowSize
            int l = Math.min(columnSize - 1, k + (width / seatWidth) + 2);//多显示1列,避免临界的突然消失的现象
            for (int j = k; j <= l; j++) {

                if (seatTable[i][j] != null) {
                    switch (seatTable[i][j].status) {
                        case -1:
                        case 0: {
                            canvas.drawBitmap(seat_sold, j * (seatWidth) + mPosX, i * (seatWidth) + mPosY, null);
                            break;
                        }
                        case 1: {
                            canvas.drawBitmap(seat_sale, j * (seatWidth) + mPosX, i * (seatWidth) + mPosY, null);
                            break;
                        }
                        case 2: {
                            canvas.drawBitmap(seat_selected, j * (seatWidth) + mPosX, i * (seatWidth) + mPosY, null);
                            break;
                        }
                        default: {
                            break;
                        }

                    }

                }
            }
        }

    }



    public void setSeatTable(SeatMo[][] seatTable) {
        this.seatTable = seatTable;
    }

    public int getRowSize() {
        return rowSize;
    }

    public void setRowSize(int rowSize) {
        this.rowSize = rowSize;
    }

    public int getColumnSize() {
        return columnSize;
    }

    public void setColumnSize(int columnSize) {
        this.columnSize = columnSize;
    }


    public void setLinePaint(Paint linePaint) {
        this.linePaint = linePaint;
    }

    public void setDefWidth(int defWidth) {
        this.defWidth = defWidth;
    }

    public int getSeatWidth() {
        return seatWidth;
    }

}

@@ -2156,160 +2156,135 @@
     
-for (
 int 
-i = 0; i %3C rowSize; i++) %7B%0A            //%E5%B1%8F%E5%B9%95%E5%A4%96,%E5%AE%8C%E6%95%B4%E7%9A%84%E8%A1%8C%E9%83%BD%E4%B8%8D%E7%BB%98%E5%88%B6%0A            if (i * (seatWidth) + mPosY %3C -seatWidth %7C%7C i * (seatWidth) + mPosY %3E 
+m = (int)(mPosY + seatWidth);%0A        m = m %3E= 0 ? 0 : -m / seatWidth;%0A        int n = Math.min(rowSize - 1, m + (
 height
-+
+ / 
 seat
@@ -2294,49 +2294,74 @@
 th) 
-%7B%0A                continue;%0A            %7D
++ 2);//%E4%B8%A4%E8%BE%B9%E5%A4%9A%E6%98%BE%E7%A4%BA1%E5%88%97,%E9%81%BF%E5%85%8D%E4%B8%B4%E7%95%8C%E7%9A%84%E7%AA%81%E7%84%B6%E6%B6%88%E5%A4%B1%E7%9A%84%E7%8E%B0%E8%B1%A1%0A        for (int i = m; i %3C= n; i++) %7B
 %0A   
@@ -2842,16 +2842,18 @@
  + 2);//
+%E4%B8%A4%E8%BE%B9
 %E5%A4%9A%E6%98%BE%E7%A4%BA1%E5%88%97,%E9%81%BF%E5%85%8D

improve draw seat