<?php

namespace App\Http\Controllers\Admin;

use App\Models\BaseAttachmentModel;
use App\Models\ClassModel;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class SettingController extends Controller
{
    protected $redirect_index = '/admin/Setting/index';

    protected $view_path = 'admin.settings.';

    protected $pre_uri = '/admin/Setting/';

    protected $model_name = '系统配置';

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new Setting();
    }

    public function share()
    {
        $share_image = $this->model->firstOrCreate(['key' => 'share_image'], ['key_show' => '图片']);
        $share_text = $this->model->firstOrCreate(['key' => 'share_text'], ['key_show' => '文本标签']);
        $share_text_pos = $this->model->firstOrCreate(['key' => 'share_text_pos'], ['key_show' => '文本位置']);

        if(!empty($share_image) && !empty($share_image->value) && Storage::disk('upload')->exists($share_image->value)) {
            $image = Image::make(url($share_image->value));
            $width = $image->width();
            $height = $image->height();
        } else {
            $width = 0;
            $height = 0;
        }

        list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);

        return view($this->view_path . 'share', compact('pre_uri', 'model', 'model_name', 'share_image', 'share_text', 'share_text_pos', 'width', 'height'));
    }

    public function updateShare(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }
        if(!empty($request->input('share_image'))) {
            $this->model->where('key', 'share_image')->update(['value' => $request->input('share_image')]);
        }
        if(!empty($request->input('share_text'))) {
            $this->model->where('key', 'share_text')->update(['value' => $request->input('share_text')]);
        }
        if(!empty($request->input('share_text_pos'))) {
            $this->model->where('key', 'share_text_pos')->update(['value' => $request->input('share_text_pos')]);
        }
        return $this->showMessage('操作成功');
    }

    public function system(Request $request)
    {
        $check_card_location = $this->model->firstOrCreate(['key' => 'check_card_location'], ['value' => '39.916527,116.397128']);
        $check_card_radius = $this->model->firstOrCreate(['key' => 'check_card_radius'], ['value' => '1000']);

        list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);

        return view($this->view_path . 'system', compact('pre_uri', 'model', 'model_name', 'check_card_location', 'check_card_radius'));
    }

    public function updateSystem(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }

        $items = collect(['check_card_location', 'check_card_radius']);

        foreach($items as $item) {
            if(!empty($request->input($item))) {
                $this->model->where('key', $item)->update(['value' => $request->input($item)]);
            }
        }
        return $this->showMessage('操作成功');
    }

    public function generateSharePhoto(Request $request)
    {
        $share_image = $this->model->where('key', 'share_image')->first();
        if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) {
            return $this->showWarning('分享图片错误或未设置');
        }
        $share_text = $this->model->where('key', 'share_text')->first();
        if(empty($share_text) || empty($share_text->value)) {
            return $this->showWarning('分享文字错误或未设置');
        }
        $share_text_pos = $this->model->where('key', 'share_text_pos')->first();
        if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) {
            return $this->showWarning('分享文字位置错误或未设置');
        }

        $text = str_replace_array('{days}', [15], $share_text->value);

        $image = Image::make(url($share_image->value));

        if($pos[0] > $image->width() || $pos[1] > $image->height()) {
            return $this->showWarning('分享文字位置错误');
        }

        $image = $image->text($text, $pos[0], $pos[1], function ($font) {
            $font->file('fonts/simsun.ttc');
            $font->size(24);
            $font->align('center');
            $font->color('#ffffff');
        });

        $size = $image->filesize();

        $name = uniqid();
        $dir = 'tmp/share/' . date("Ymd");
        $path = 'tmp/share/' . date("Ymd") . '/' . $name . '.png';
        $full_path = '/' . $path;
        if(!\File::isDirectory($dir)) {
            \File::makeDirectory($dir, 493, true);
        }

        $items = BaseAttachmentModel::where('class', '分享图片测试')->get();

        foreach($items as $item) {
            if(Storage::disk('upload')->exists($item->path)) {
                Storage::disk('upload')->delete($item->path);
            }
            $item->delete();
        }

        $image->save($path);

        BaseAttachmentModel::create([
            'path' => $full_path,
            'url' => $full_path,
            'name' => $name,
            'class' => '分享图片测试',
            'size' => $size,
        ]);

        ClassModel::firstOrCreate(['class' => '分享图片测试']);

        return view($this->view_path . 'share-show', compact('full_path'));
    }
}
