using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using AppClass;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Form1
{
    public partial class Form2 : Form
    {

        


        public Form2()
        {
            InitializeComponent();
        }


        bool zhixingbol = false;
        App app = new App();
        Thread thread = null;
        string appId;
        int aw, ah;



        private void Form2_Load(object sender, EventArgs e)
        {
            this.appId = this.Text;

            pictureBox1.Width = 300;
            pictureBox1.Height = 600;

            app.setAppId(appId);                        //设置手机ID
            app.setPictureBox(pictureBox1);             //设置绑定pictureBox控件

            this.aw = app.getAppW();
            this.ah = app.getAppH();
            this.Text += "[" + this.aw + "x" + this.ah + "]";
            
            button1.PerformClick();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread != null && thread.IsAlive) thread.Abort();
            app.guanbiCmd();
        }


        bool movebol = true; //true表示单击,false表示滑动
        int pX, pY;  //pictureBox1_MouseUp用参
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            movebol = false;

            label1.Text = e.X + ":" + e.Y;

            if (e.X > pictureBox1.Width - 50) label1.Left = e.X + pictureBox1.Left - 50;
            else label1.Left = e.X + pictureBox1.Left + 20;

            label1.Top = e.Y;// + pictureBox1.Top;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pX = e.X;pY = e.Y;
            movebol = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (zhixingbol) return; //如果在执行中，禁用

            this.Invoke(new MethodInvoker(delegate () {
                if (movebol) app.DianJi(pX, pY);
                else app.SHua(pX, pY, e.X, e.Y);
            }));
        }




        /// <summary>
        /// 执行、暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "停止中")
            {
                button1.Text = "执行中";
                button1.BackColor = Color.Blue;
                zhixingbol = true;
                RenWu();
            }
            else
            {
                button1.Text = "停止中";
                button1.BackColor = Color.Red;
                zhixingbol = false;  //只执行获取图
            }
        }




        string[] shezhi;
        DateTime shezhi_dtime;
        DateTime fanqixiaoshuo_dxy_dtime;
        Dictionary<string, ArrayList> fanqixiaoshuo_darray;


        /// <summary>
        /// 任务线程
        /// </summary>
        private void RenWu()
        {
            thread = new Thread(() => {
                //pictureBox1绑定天使插件
                if (app.bdTS())
                {
                    while (true)
                    {
                        huoquTU();
                        if (!zhixingbol)
                        {
                            Task.Delay(1000).Wait();
                            continue;
                        }
                        

                        DateTime dtime = File.GetLastWriteTime("shezhi.txt");
                        
                        if (this.shezhi_dtime != dtime) //判断shezhi.txt是否右更新
                        {
                            try
                            {
                                this.shezhi = File.ReadAllLines("shezhi.txt");
                                this.shezhi_dtime = dtime;
                            }
                            catch 
                            {
                            }
                        }

                        // 00点重置
                        fanqixiaoshuo_chongzhi();


                        //执行任务
                        foreach (string lines in this.shezhi)
                        {
                            if (lines.Split('=')[1] != string.Empty)
                            {
                                int sn = int.Parse(lines.Split('=')[0].Trim());
                                string[] ttime = lines.Split('=')[1].Split('|');
                                foreach (string times in ttime)
                                {
                                    string[] s = times.Split('-');
                                    if (fanweiTime(DateTime.Parse(s[0] + ":00"), DateTime.Parse(s[1] + ":00")))
                                    {
                                        switch(sn)
                                        {
                                            case 0:
                                                this.Invoke(new MethodInvoker(delegate () {
                                                    label2.Text = "番茄小说--看书";
                                                }));
                                                fanqixiaoshuo_kanshu();
                                                break;

                                            case 1:
                                                this.Invoke(new MethodInvoker(delegate () {
                                                    label2.Text = "番茄小说--收金";
                                                }));
                                                fanqixiaoshuo_shoujin();
                                                break;

                                            case 2:
                                                this.Invoke(new MethodInvoker(delegate () {
                                                    label2.Text = "7猫小说--看书";
                                                }));
                                                qimaoxiaoshuo_kanshu();
                                                break;

                                            case 3:
                                                this.Invoke(new MethodInvoker(delegate () {
                                                    label2.Text = "7猫小说--收金";
                                                }));
                                                qimaoxiaoshuo_shoujin();
                                                break;

                                            case 4:
                                                break;

                                            case 5:
                                                break;

                                            case 6:
                                                break;

                                            case 7:
                                                break;
                                        }
                                    }
                                }
                            }
                        }


                        

                    }


                }
                else {
                    MessageBox.Show("绑定失败");
                }

            });
            thread.Start();

        }



        /// <summary>
        /// 获取颜色坐标文件
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, ArrayList> fanqixiaoshuo_dxy(string imgs)
        {
            string[] dxy;
            Dictionary<string, ArrayList> darr = new Dictionary<string, ArrayList>();
            DateTime dtime = File.GetLastWriteTime(imgs + "dxy.txt");
            if (this.fanqixiaoshuo_dxy_dtime != dtime)  //判断dxy.txt是否右更新
            {
                this.fanqixiaoshuo_dxy_dtime = dtime;
                dxy = File.ReadAllLines(imgs + "dxy.txt");

                foreach (string lines in dxy)
                {
                    if (lines.StartsWith("#") || lines == string.Empty) continue; //是注释 或 空白，跳过

                    string[] sp = lines.Split('=');
                    if (sp.Length >= 2 && sp[1] != string.Empty)
                    {
                        if (darr.Keys.Contains(sp[0]))
                        {
                            darr[sp[0]].Add(sp[1]);
                        }
                        else
                        {
                            darr.Add(sp[0], new ArrayList());
                            darr[sp[0]].Add(sp[1]);
                        }
                    }
                }

                this.fanqixiaoshuo_darray = darr;
            }

            return this.fanqixiaoshuo_darray;

        }





        /**************************************
         * 番茄小说
         **************************************/
         
        int fanqixiaoshuo_shoujin_huadong_num = 0;    //收金上下滑动计数
        int fanqixiaoshuo_shoujin_baoxiang_num = 10;  //宝箱每天次数
        bool fanqixiaoshuo_shoujin_bool = true;       
        bool fanqixiaoshuo_kanshu_bool = true;
        DateTime fanqixiaoshuo_shoujin_kanshipin_dtime;  //看视频间隔时间


        /// <summary>
        /// 00点重置
        /// </summary>
        private void fanqixiaoshuo_chongzhi()
        {
            if (DateTime.Now.Hour == 0)
            {
                this.fanqixiaoshuo_shoujin_baoxiang_num = 10;
            }
        }

        /// <summary>
        /// 广告取消
        /// </summary>
        /// <param name="renwuming">任务名：shoujin, kanshu</param>
        private void fanqixiaoshuo_guanggao(string renwuming)
        {
            string ztm = string.Empty; //状态码，区分当前情况
            string imgss = string.Empty;
            string imgs = "tsimgs\\" + this.aw + "x" + this.ah + "\\番茄小说\\";
            int[] n1, n2, n3, n4, n5, n6, n7;

            Dictionary<string, ArrayList>  darr = fanqixiaoshuo_dxy(imgs);


            imgss = imgs + "书城0.bmp|" +
                    imgs + "书城1.bmp";
            n1 = app.tuXY2(0, 500, 100, 570, imgss, 0.8);

            imgss = imgs + "看书页a.bmp|" +
                    imgs + "看书页b.bmp";
            n2 = app.tuXY2(0, 0, 60, 60, imgss, 0.8);

            imgss = imgs + "功能键ffffff.bmp";
            n3 = app.tuXY2(0, 550, 300, 600, imgss, 0.9);

            imgss = imgs + "功能键f1f3f5.bmp";
            n4 = app.tuXY2(0, 550, 300, 600, imgss, 0.9);

            imgss = imgs + "功能键000000.bmp";
            n5 = app.tuXY2(0, 550, 300, 600, imgss, 0.9);

            imgss = imgs + "功能键cccccc.bmp";
            n6 = app.tuXY2(0, 550, 300, 600, imgss, 0.9);

            imgss = imgs + "功能键666666.bmp";
            n7 = app.tuXY2(0, 550, 300, 600, imgss, 0.9);



            
            if (n1[0] == 1 && n2[0] == 0 && n3[0] == 1)
            {
                ztm = "1010000";//书城=1, 功能键-白色ffffff
            }
            else if (n1[0] == 0 && n2[0] == 0 && n3[0] == 1)
            {
                ztm = "0010000";//功能键-白色ffffff
            }
            else if (n1[0] == 0 && n2[0] == 0 && n4[0] == 1)
            {
                ztm = "0001000";//功能键-灰白色f1f3f5
            }
            else if (n1[0] == 0 && n2[0] == 0 && n5[0] == 1)
            {
                ztm = "0000100";//功能键-黑色000000
            }
            else if (n1[0] == 0 && n2[0] == 0 && n6[0] == 1)
            {
                ztm = "0000010";//功能键-灰色cccccc
            }
            else if (n1[0] == 0 && n2[0] == 0 && n7[0] == 1)
            {
                ztm = "0000001";//功能键-灰黑色666666
            }
            else if (n1[0] == 0 && n2[0] == 0 && n3[0] == 0 && n4[0] == 0 && n5[0] == 0 && n6[0] == 0 && n7[0] == 0)
            {
                //无功能键，无书架，无看书
                //可能是视频
                ztm = "0000000";
            }


            if (ztm != string.Empty && darr.Keys.Contains(renwuming + ztm))
            {
                ArrayList arr = darr[renwuming + ztm];
                foreach (string s in arr)
                {
                    string[] ss = s.Split('@');
                    string[] sexy = ss[0].Split('&');
                    bool bol = false;
                    if (sexy.Length == 1) 
                    {
                        string[] sexy1 = sexy[0].Split(',');
                        bol = app.sfSE(int.Parse(sexy1[0].Trim()), int.Parse(sexy1[1].Trim()), sexy1[2].Trim()) ? true : false;
                            
                    }

                    if (sexy.Length == 2)
                    {
                        string[] sexy1 = sexy[0].Split(',');
                        string[] sexy2 = sexy[1].Split(',');

                        bol = (app.sfSE(int.Parse(sexy1[0].Trim()), int.Parse(sexy1[1].Trim()), sexy1[2].Trim()) &&
                            app.sfSE(int.Parse(sexy2[0].Trim()), int.Parse(sexy2[1].Trim()), sexy2[2].Trim())) ? true : false;
                        
                    }

                    if (sexy.Length == 3)
                    {
                        string[] sexy1 = sexy[0].Split(',');
                        string[] sexy2 = sexy[1].Split(',');
                        string[] sexy3 = sexy[2].Split(',');

                        bol = (app.sfSE(int.Parse(sexy1[0].Trim()), int.Parse(sexy1[1].Trim()), sexy1[2].Trim()) &&
                            app.sfSE(int.Parse(sexy2[0].Trim()), int.Parse(sexy2[1].Trim()), sexy2[2].Trim()) &&
                            app.sfSE(int.Parse(sexy3[0].Trim()), int.Parse(sexy3[1].Trim()), sexy3[2].Trim())) ? true : false;
                        
                    }


                    if (bol)
                    { 
                        if (ss[1].Trim() == "F")
                        {
                            app.FanHuiJian();  //返回键
                        }
                        else if (ss[1].Trim() == "H")
                        {
                            app.HomeJian();   //Home键
                        }
                        else if (ss[1].Trim() == "G")
                        {
                            app.guanbiAPP("com.dragon.read");  //关闭app
                        }
                        else
                        {
                            string[] sss = ss[1].Split(',');
                            if (sss[0].Trim() == "D") Task.Delay(int.Parse(sss[1].Trim())).Wait();  //延时指定时间
                            else app.DianJi(int.Parse(sss[0].Trim()), int.Parse(sss[1].Trim()));    //点击指定位置

                        }
                    }
                    


                }
            }

        }

        /// <summary>
        /// 收金
        /// </summary>
        private void fanqixiaoshuo_shoujin()
        {
            string imgss = string.Empty;
            string imgs = "tsimgs\\" + this.aw + "x" + this.ah + "\\番茄小说\\";
            int[] n,nn;

            imgss = imgs + "拨电话.bmp";
            n = app.tuXY(0, 490, 300, 600, imgss);
            if (n[0] == 1)
            {
                //是桌面，进入番茄小说
                app.dakaiAPP("com.dragon.read",".pages.splash.SplashActivity");

                this.fanqixiaoshuo_shoujin_bool = false;
                Task.Delay(5000).Wait();
                return;
            }
            else if (this.fanqixiaoshuo_shoujin_bool)
            {
                this.fanqixiaoshuo_shoujin_bool = false;
                this.fanqixiaoshuo_kanshu_bool = true;
                app.HomeJian();
                app.guanbiAPP("com.dragon.read");
                return;
            }
            

            imgss = imgs + "书城0.bmp|"+
                    imgs + "书城1.bmp";
            n = app.tuXY(0, 500, 100, 570, imgss);
            if (n[0] == 1)
            {
                imgss = imgs + "福利页.bmp";
                nn = app.tuXY(0, 0, 100, 80, imgss);
                if (nn[0] == 0)
                {
                    //不在福利页
                    app.DianJi(n[1]+120, n[2]+10);
                    Task.Delay(5000).Wait();

                    fanqixiaoshuo_guanggao("shoujin");

                    return;//退出函数，判断界面
                }
                else
                {
                    //在福利页

                    if (this.fanqixiaoshuo_shoujin_baoxiang_num > 0)
                    { 
                        imgss = imgs + "宝箱.bmp";
                        n = app.tuXY(0, 300, 300, 600, imgss);
                        if (n[0] == 1) 
                        { 
                            app.DianJi(n[1], n[2] - 20);
                            this.fanqixiaoshuo_shoujin_baoxiang_num--;
                            return;
                        }

                    }
                    

                    
                    imgss = imgs + "去签到.bmp";
                    n = app.tuXY2(200, 50, 300, 400, imgss,0.5);
                    if (n[0] == 1 && app.sfSE(n[1]-53, n[2]+120, "fff1d4"))
                    {
                        app.DianJi(n[1], n[2]);
                        return;//阻止滑动
                    }


                    //看书，听书，领金币
                    imgss = imgs + "阅读赚.bmp|" +
                            imgs + "听书赚.bmp";
                    n = app.tuXY2(0, 50, 150, 400, imgss, 0.5);
                    if (n[0] == 1 && app.sfSE(n[1]+224, n[2]+16, "fa6725"))
                    {
                        app.DianJi(n[1], n[2]);
                        return;//阻止滑动
                    }

                    //看视频，领金币
                    if (DateTime.Now > this.fanqixiaoshuo_shoujin_kanshipin_dtime)
                    {
                        imgss = imgs + "看视频.bmp";
                        n = app.tuXY2(0, 50, 150, 400, imgss, 0.6);
                        if (n[0] == 1)
                        {
                            app.DianJi(n[1]+246, n[2]+16);
                            this.fanqixiaoshuo_shoujin_kanshipin_dtime = DateTime.Now.AddMinutes(10);  //保存当前时间加5分钟，内不在执行
                            return;//阻止滑动
                        }
                    }





                    if (this.fanqixiaoshuo_shoujin_huadong_num < 14 && (this.fanqixiaoshuo_shoujin_huadong_num % 2) == 0) app.ShangHua2();
                    else if((this.fanqixiaoshuo_shoujin_huadong_num % 2) == 0) app.XiaHua2();
                    if (this.fanqixiaoshuo_shoujin_huadong_num > 30 ) this.fanqixiaoshuo_shoujin_huadong_num = 0;
                    this.fanqixiaoshuo_shoujin_huadong_num++;
                    

                }


            }
            
            fanqixiaoshuo_guanggao("shoujin");
            Task.Delay(1000).Wait();
        }

        /// <summary>
        /// 看书
        /// </summary>
        private void fanqixiaoshuo_kanshu()
        {
            string imgss = string.Empty;
            string imgs = "tsimgs\\" + this.aw + "x" + this.ah + "\\番茄小说\\";
            int[] n, nn, nnn;

            imgss = imgs + "拨电话.bmp";
            n = app.tuXY(0, 490, 300, 600, imgss);
            if (n[0] == 1)
            {
                //是桌面，进入番茄小说
                app.dakaiAPP("com.dragon.read", ".pages.splash.SplashActivity");

                this.fanqixiaoshuo_kanshu_bool = false;
                Task.Delay(5000).Wait();
                return;
            }
            else if (this.fanqixiaoshuo_kanshu_bool)
            {
                this.fanqixiaoshuo_shoujin_bool = true;
                this.fanqixiaoshuo_kanshu_bool = false;
                app.HomeJian();
                app.guanbiAPP("com.dragon.read");
                return;

            }


            imgss = imgs + "书城0.bmp|" +
                    imgs + "书城1.bmp";
            n = app.tuXY2(0, 500, 100, 570, imgss, 0.6);
            if (n[0] == 1)
            {
                imgss = imgs + "书架页.bmp";
                nn = app.tuXY(0, 0, 100, 80, imgss);
                if (nn[0] == 0)
                {
                    //不在书架页
                    app.DianJi(n[1] + 177, n[2] + 10);
                    Task.Delay(5000).Wait();
                    app.DianJi(55, 180);

                    fanqixiaoshuo_guanggao("kanshu");

                    return;//退出函数，判断界面
                }
            }

            imgss = imgs + "看书页a.bmp";
            n = app.tuXY(0, 0, 50, 60, imgss);
            imgss = imgs + "看书页b.bmp";
            nn = app.tuXY(0, 0, 50, 60, imgss);
            imgss = imgs + "听.bmp";
            nnn = app.tuXY(0, 150, 300, 600, imgss);
            if (n[0] == 1 && nnn[0] == 1)
            {
                app.DianJi(nnn[1] + 2, nnn[2] + 2);
                Task.Delay(5000).Wait();
                app.FanHuiJian();
            }
            else if (nn[0] == 1)
            {
                app.DianJi(150, 300);
            }
            else if (n[0] == 1) 
            {
                Task.Delay(5000).Wait();
            }
            






            //imgss = imgs + "看书页a.bmp|" +
            //        imgs + "看书页b.bmp";
            //n = app.tuXY(0, 0, 50, 60, imgss);
            //if (n[0] == 1)
            //{
            //if (suijiShu(5)) Task.Delay(5000).Wait();
            //else if (suijiShu(5)) Task.Delay(8000).Wait();
            //else Task.Delay(12000).Wait();


            //if (suijiShu(5))//50%机率滑动
            //{
            //    if (app.sfSE(20, 80, "ded9c5"))
            //    {
            //        if (suijiShu(8))
            //        {
            //            app.ZuoHua(); //80%机率左滑
            //        }
            //        else
            //        {
            //            app.YouHua(); //20机率右滑
            //        }
            //    }
            //    else
            //    {
            //        //大概是广告
            //        app.ZuoHua2();
            //        app.ZuoHua();
            //    }
            //}


            //}

            
            fanqixiaoshuo_guanggao("kanshu");

        }





        /**************************************
         * 7猫小说
         **************************************/

        
        private void qimiaoxiaoshuo_guanggao()
        { 
        
        }


        private void qimaoxiaoshuo_shoujin() 
        {
        
        }


        private void qimaoxiaoshuo_kanshu()
        {

        }





        //==============================================================================
        /// <summary>
        /// 获取APP图
        /// </summary>
        private void huoquTU()
        {
            app.huoquTU();
            FileStream fs = File.OpenRead("adbimgs\\" + appId + ".bmp");
            this.Invoke(new MethodInvoker(delegate ()
            {
                pictureBox1.Image = Image.FromStream(fs);
            }));
            fs.Close();  //确保png不被占用
            fs.Dispose();

            //Image originalImage = Image.FromFile("adbimgs\\" + appId + ".bmp");
            //using (Bitmap resizedImage = new Bitmap(pictureBox1.Width, pictureBox1.Height)) {
            //    using (Graphics graphics = Graphics.FromImage(resizedImage)) { 
            //        graphics.SmoothingMode = SmoothingMode.HighQuality; // 设置平滑模式为最佳质量
            //        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // 设置插值模式为双三次插值
            //        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; // 设置像素偏移模式为最佳质量
            //        graphics.CompositingQuality = CompositingQuality.HighQuality; // 设置合成质量为最佳质量
            //        graphics.Clear(Color.Transparent); // 清除画布
            //        graphics.DrawImage(originalImage, 0, 0, pictureBox1.Width, pictureBox1.Height); // 绘制原始图像到新的位图上
            //    }
            //    this.Invoke(new MethodInvoker(delegate ()
            //    {
            //        pictureBox1.Image = Image.FromHbitmap(resizedImage.GetHbitmap());
            //        pictureBox1.Refresh();
            //    }));
            //}
            //originalImage.Dispose();
        }


        /// <summary>
        /// 当前时间，是否在指定范围
        /// </summary>
        /// <param name="t1">时间启</param>
        /// <param name="t2">时间止</param>
        /// <returns></returns>
        private bool fanweiTime(DateTime t1, DateTime t2) {

            if (t1.CompareTo(DateTime.Now) <= 0 
                && t2.CompareTo(DateTime.Now) > 0) {
                return true;
            }
            return false;
        }

       


        /// <summary>
        /// 随机执行，0-9
        /// </summary>
        /// <param name="n">指定数值内返回true</param>
        /// <returns></returns>
        private bool suijiShu(int n) {
            Random random = new Random();
            return random.Next(10)<=n?true:false;
        }

        







        //----------------------------------------------------
        private void TiaoShi() 
        {
            //int[] n = app.tuXY(0, 0, 300, 400, "tsimgs\\番茄小说\\签到.bmp");
            //MessageBox.Show(n[0].ToString() + "/" + n[1] + ":"+n[2]);
        }

    }
}
