package cn.bunny.pattern4.demo3;

import lombok.Setter;

public class CameraDemo {
    public static void main(String[] args) {
        TakePhotoCamera takePhotoCamera = new TakePhotoCamera();
        takePhotoCamera.operation();

        System.out.println("\n-----------------分割线-----------------\n");

        BeautyCamera beautyCamera = new BeautyCamera(takePhotoCamera);
        beautyCamera.operation();

        System.out.println("\n-----------------分割线-----------------\n");

        FilterCamera filterCamera = new FilterCamera(beautyCamera);
        filterCamera.operation();

        System.out.println("\n-----------------分割线-----------------\n");

        TimerCamera timerCamera = new TimerCamera();
        timerCamera.setTime(2);
        timerCamera.operation();
        filterCamera = new FilterCamera(beautyCamera);
        filterCamera.operation();
    }

    // 相机功能
    interface Camera {

        /**
         * 相机操作
         */
        void operation();
    }

    // 相机装饰者角色
    static abstract class CameraDecorator implements Camera {
        Camera camera;

        public CameraDecorator(Camera camera) {
            this.camera = camera;
        }
    }

    // 相机拍照
    static class TakePhotoCamera implements Camera {

        /**
         * 相机操作
         */
        @Override
        public void operation() {
            System.out.println("相机拍照。。。");
        }
    }

    // 相机功能
    static class BeautyCamera extends CameraDecorator {

        public BeautyCamera(Camera camera) {
            super(camera);
        }

        /**
         * 相机操作
         */
        @Override
        public void operation() {
            camera.operation();
            System.out.println("美颜功能。。。");
        }
    }

    // 滤镜效果
    static class FilterCamera extends CameraDecorator {

        public FilterCamera(Camera camera) {
            super(camera);
        }

        /**
         * 相机操作
         */
        @Override
        public void operation() {
            camera.operation();
            System.out.println("滤镜效果。。。");
        }
    }

    // 实现相机延迟功能
    @Setter
    static class TimerCamera implements Camera {

        // 延迟时间
        private Integer time = 0;

        /**
         * 相机操作
         */
        @Override
        public void operation() {
            System.out.println("开始拍照，延迟时间：" + time + "s");
            try {
                Thread.sleep(time * 1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
